Upload
sangwon-ko
View
980
Download
16
Embed Size (px)
DESCRIPTION
IoC and DI Pattern, 요즘엔 고전 of 고전이 된 내용
Citation preview
Inversion of Control Containers and the Dependency Injection pattern
문제점
주류 J2EE 의 문제 : heavyweight complexity
※ 고민 : 어떻게 서로 다른 요소를 같이 엮어서 쓸 수 있을까? ex) 각자 만든 web controller architecture와 Database Interface를 어떻게 같이 사용할 수 있을까?
대안은 Open source 다양한 Lightweight Container 출현
예제 (1/4) 특정 감독이 연출한 영화의 목록을 찾아주는 컴포넌트
예제 (2/4) MovieLister와 특정 finder 객체를 연결하는 방법
예제 (3/4) 생성자에서 구현된 클래스 생성
예제 (4/4)
[The dependencies using a simple creation in the lister class]
1. lister 객체가 어떤 Impl도 사용할 수 있고, 2. 나중에 그 Impl을 특별한 수정 없이 사용할 수 있으면 좋겠다.
Plugin Pattern
Plugin : http://martinfowler.com/eaaCatalog/plugin.html
Links classes during configuration rather than compilation.
핵심
여러 플러그인들을 어떻게 조립해서 한 애플리케이션으로 만들 수 있을까?
Inversion of Control
Inversion of Control
핵심은 어떤 제어를 역전시키려고 했는가? Plug-in Implementation을 찾는 방법 = implementation이 Injection 가능하도록 함
Inversion of Control는 프레임워크의 공통된 특성,
Dependency Injection
Why?
Dependency Injection
[The dependencies for a Dependency Injector]
Dependency Injection
Three main styles of dependency injection :
• Constructor Injection
• Setter Injection
• Interface Injection
Constructor Injection
PicoContainer https://github.com/picocontainer/picocontainer
Constructor Injection ※ 각 인터페이스마다 어떤 클래스를 관련 지을지, 어떤 문자열을 Injection할지 알려줘야 함
Spring http://www.springsource.org
Setter Injection
Setter Injection
Interface Injection
Avalon http://avalon.apache.org/closed.html Excalibur(http://excalibur.apache.org) retired (2010/12/15)
Interface Injection
Using a Service Locator
[The dependencies for a Service Locator]
- Dependency를 없앨 수 있는 다른 방법 - 응용 프로그램이 필요로 할 서비스들의 전체 소유권을 어떻게 취할지 알고 있는 어떤 객체를 두자는 것
Using a Service Locator
Using a Service Locator
Using a Segregated Interface for the Locator
Separated Interface 적용 – ISP(Interface segregation principle)
A Dynamic Service Locator
Using both a locator and injection with Avalon
Avalon은 Service Locator와 Dependency Injection 모두 사용 가능
Deciding which option to use
Service Locator Dependency Injection
장점 - 개발자에게 더 직접적인 표현 - 명시적인 호출이 없음 - 의존관계 파악이 쉬움 - 테스트 쉬움
단점
- 모든 객체가 Service Locator에 의존 - 의존관계 파악이 어려움 - Service Locator API에 잘 알고 있어야 함 - 테스트가 상대적으로 어려움
- 컴포넌트가 추가적인 요청을 할 수 없음
※ Interface Injection 탈락 사유 : 인터페이스를 만들어야 됨. 상대적으로 많은 작업 필요
Constructor Injection Setter Injection
장점 - Field 를 숨길 수 있음 - 객체 생성에 대한 명확한 의도 표현
간단하고 쉬움
단점
- 매개변수가 많아지면 코드가 지저분 - 생성 방법이 여러 가지인 경우, 표현이 어려움 - 위치를 통해 매개변수의 용도를 알기 어려움 - 상속인 경우 부모클래스 + 자기 매개변수
Immutable field에 대한 신뢰
Constructor vs Setter Injection
Service Locator vs Dependency Injection
Code or configuration files
XML hell-!!
Compile!!
ex) API vs XML
개발자에겐 프로그래밍 언어가 더 익숙.. 둘 다 지원하고, 선택은 사용자에게 맡겨라.
Annotation
Spring 3.x, Annotation-based configuration
- Example : @Autowired
JSR 330: Dependency Injection for Java http://jcp.org/en/jsr/detail?id=330
google-guice http://code.google.com/p/google-guice
PicoContainer https://github.com/picocontainer
그 외..
Java Spec
DI Frameworks
Spring http://www.springsource.org