티스토리 뷰

Framework/Spring

[Spring & ibatis] 수업 2일

gray.yoon 2013. 4. 23. 19:37

수업 1일차 review

 

Inheritance(상속)

why?

- 객체(멤버) 재사용성

- 계층 구조 형성(다형성(Polymorphism) 적용)    "One interface Multiple implements"

- 표준화

 

 

Abstrac(추상 클래스)

- 재사용성

- 단일

 

Interface(인터페이스)

- 재사용성X

- 다중

 

공통점 - 표준화

 

** 객체지향 주요개념 **

1. Encapsulation

     "public interface, private implementation"

구현부 변경되어도 외부 커뮤니케이션 지장없다. -> 유지보수성

사용자 측은 구현부 알 필요 없이 인터페이스만 알면 된다 -> 사용자 편의성

 

2. Inheritance

상속 : 재사용성과 계층 구조 형성 -> 다형성(polymorphism) 적용 환경을 제공

 

3. Polymorphism

다형성 : "One interface, Multiple implements"

 

Spring DI

계층간 결합도를 낮추기 위한 방안  1. DL (dependency lookup)

2. DI (dependency injection)

 

DI(IOC)

spring container가 구체적인 객체를 생성해 어플리케이션으로 주입하여 결합도를 낮춘다.

변경 소스 수정 필요 없이 설정만 수정하면 된다.

 


 

수업 2일차

 

association : use a 사람 - PC방의 PC 관계 (쓰고 버럼)

aggregation : has a 사람 - 스마트폰 관계 (소유물) - 없을 수도 있음 (setter method)

composition : consist of - 사람과 뇌의 관계 (필수) - 생성자

 

 

TEST

 

 

위와 같은 구조로 어플리케이션 Test

 

Book.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package model;
 
public class Book {
    private String title;
    private String writer;
    public Book(String title, String writer) {
        super();
        this.title = title;
        this.writer = writer;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getWriter() {
        return writer;
    }
    public void setWriter(String writer) {
        this.writer = writer;
    }
    @Override
    public String toString() {
        return "Book [title=" + title + ", writer=" + writer + "]";
    }
    
}

 

Dormitory.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package model;
import java.util.List;
public class Dormitory implements Residence{
    private String schoolName;
    private List<Book> library;
    
    public String getSchoolName() {
        return schoolName;
    }
    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }
    public List<Book> getLibrary() {
        return library;
    }
    public void setLibrary(List<Book> library) {
        this.library = library;
    }
    @Override
    public void reside() {
        System.out.println(schoolName+" "+library);
    }    
}

 

HomeAppliance.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package model;
 
public class HomeAppliance {
    private String maker;
    private String productName;
    public String getMaker() {
        return maker;
    }
    public void setMaker(String maker) {
        this.maker = maker;
    }
    public String getProductName() {
        return productName;
    }
    public void setProductName(String productName) {
        this.productName = productName;
    }
    @Override
    public String toString() {
        return "HomeAppliance [maker=" + maker + ", productName=" + productName
                + "]";
    }
}
 

 

 

Hotel.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package model;
 
public class Hotel implements Residence {
    private String hotelName;
    private int cost;
    public String getHotelName() {
        return hotelName;
    }
    public void setHotelName(String hotelName) {
        this.hotelName = hotelName;
    }
    public int getCost() {
        return cost;
    }
    public void setCost(int cost) {
        this.cost = cost;
    }
    @Override
    public void reside() {
        System.out.println(hotelName+" "+cost+"만원");
    }
 
}
 

 

 

House.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package model;
 
import java.util.Map;
 
public class House implements Residence{
    private String address;
    private Map<String, HomeAppliance> hp;
    
    public House(Map<String, HomeAppliance> hp) {
        super();
        this.hp = hp;
    }
    
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    public Map<String, HomeAppliance> getHp() {
        return hp;
    }
 
    public void setHp(Map<String, HomeAppliance> hp) {
        this.hp = hp;
    }
 
    @Override
    public void reside() {
        System.out.println(address+" "+hp);
    }
    
}
 

 

Residence.java

1
2
3
4
5
6
package model;
 
public interface Residence {
    public void reside();
}
 

 

TestDI.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package test;
 
import model.Residence;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
 
public class TestDI {
    /**
     * 
     * OOAD
     * association     : use a             사람        렌트카        지역변수
     * aggregation     : has a            사람        스마트폰    맴버변수 setter주입
     * composition    : consist of     자동차    엔진            lifecycle 생성자를 통한 주입
     * 
     * 
     * association > aggregation > composition
     * 
     * 힐튼 30만원
     * 
     * 학교명:판교대학교 서고:[Book [title=웹2.0경제학, writer=김국현], Book [title=새로운미래가온다,
     * writer=다니엘핑크]]
     * 
     * 판교자이 {tv=HomeAppliance [maker=삼성, productName=파브], washing=HomeAppliance
     * [maker=LG, productName=세탁기]}
     * 
     * @param args
     */
 
    public static void main(String[] args) {
        Resource r = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(r);
        Residence r1 = (Residence) factory.getBean("hotel");
        r1.reside();
        Residence r2 = (Residence) factory.getBean("dormitory");
        r2.reside();
        Residence r3 = (Residence) factory.getBean("house");
        r3.reside();
    }
}

 

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="hotel" class="model.Hotel">
        <property name="hotelName">
            <value>힐튼</value>
        </property>
        <property name="cost">
            <value>33</value>
        </property>
    </bean>
    
    <!--     book*2
            Dormitory     schoolName과 library list(book1,book2)
     -->
     <bean id="book1" class="model.Book">
        <constructor-arg>
            <value>웹2.0경제학</value>
        </constructor-arg>
        <constructor-arg>
            <value>김국현</value>
        </constructor-arg>
    </bean>
    <bean id="book2" class="model.Book">
        <constructor-arg>
            <value>새로운 미래가 온다</value>
        </constructor-arg>
        <constructor-arg>
            <value>다니엘 핑크</value>
        </constructor-arg>
    </bean>
    <bean id="dormitory" class="model.Dormitory">
        <property name="schoolName">
            <value>판교대학교</value>
        </property>
        <property name="library">
            <list>
                <ref bean="book1"/>
                <ref bean="book2"/>
            </list>
        </property>
    </bean>
    
    <bean id="hp1" class="model.HomeAppliance">
        <property name="maker" value="삼성"/>
        <property name="productName" value="파브"/>    
    </bean>
    <bean id="hp2" class="model.HomeAppliance">
        <property name="maker" value="LG"/>
        <property name="productName" value="세탁기"/>    
    </bean>
    
    <bean id="house" class="model.House">
        <property name="address" value="판교자이"/>
        <constructor-arg>
            <map>
                <entry>
                    <key><value>tv</value></key>
                    <ref bean="hp1"/>
                </entry>
                <entry>
                    <key><value>washing</value></key>
                    <ref bean="hp2"/>
                </entry>
            </map>
        </constructor-arg>
    </bean>
    
</beans>
 

 

3일차 교육 예정
java design pattern
DI
Log4j
AOP

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함