27
Inversion of Control Containers and the Dependency Injection pattern

IoC and DI Pattern

Embed Size (px)

DESCRIPTION

IoC and DI Pattern, 요즘엔 고전 of 고전이 된 내용

Citation preview

Page 1: IoC and DI Pattern

Inversion of Control Containers and the Dependency Injection pattern

Page 2: IoC and DI Pattern

문제점

주류 J2EE 의 문제 : heavyweight complexity

※ 고민 : 어떻게 서로 다른 요소를 같이 엮어서 쓸 수 있을까? ex) 각자 만든 web controller architecture와 Database Interface를 어떻게 같이 사용할 수 있을까?

대안은 Open source 다양한 Lightweight Container 출현

Page 3: IoC and DI Pattern

예제 (1/4) 특정 감독이 연출한 영화의 목록을 찾아주는 컴포넌트

Page 4: IoC and DI Pattern

예제 (2/4) MovieLister와 특정 finder 객체를 연결하는 방법

Page 5: IoC and DI Pattern

예제 (3/4) 생성자에서 구현된 클래스 생성

Page 6: IoC and DI Pattern

예제 (4/4)

[The dependencies using a simple creation in the lister class]

1. lister 객체가 어떤 Impl도 사용할 수 있고, 2. 나중에 그 Impl을 특별한 수정 없이 사용할 수 있으면 좋겠다.

Page 7: IoC and DI Pattern

Plugin Pattern

Plugin : http://martinfowler.com/eaaCatalog/plugin.html

Links classes during configuration rather than compilation.

Page 8: IoC and DI Pattern

핵심

여러 플러그인들을 어떻게 조립해서 한 애플리케이션으로 만들 수 있을까?

Inversion of Control

Page 9: IoC and DI Pattern

Inversion of Control

핵심은 어떤 제어를 역전시키려고 했는가? Plug-in Implementation을 찾는 방법 = implementation이 Injection 가능하도록 함

Inversion of Control는 프레임워크의 공통된 특성,

Dependency Injection

Why?

Page 10: IoC and DI Pattern

Dependency Injection

[The dependencies for a Dependency Injector]

Page 11: IoC and DI Pattern

Dependency Injection

Three main styles of dependency injection :

• Constructor Injection

• Setter Injection

• Interface Injection

Page 12: IoC and DI Pattern

Constructor Injection

PicoContainer https://github.com/picocontainer/picocontainer

Page 13: IoC and DI Pattern

Constructor Injection ※ 각 인터페이스마다 어떤 클래스를 관련 지을지, 어떤 문자열을 Injection할지 알려줘야 함

Page 14: IoC and DI Pattern

Spring http://www.springsource.org

Setter Injection

Page 15: IoC and DI Pattern

Setter Injection

Page 16: IoC and DI Pattern

Interface Injection

Avalon http://avalon.apache.org/closed.html Excalibur(http://excalibur.apache.org) retired (2010/12/15)

Page 17: IoC and DI Pattern

Interface Injection

Page 18: IoC and DI Pattern

Using a Service Locator

[The dependencies for a Service Locator]

- Dependency를 없앨 수 있는 다른 방법 - 응용 프로그램이 필요로 할 서비스들의 전체 소유권을 어떻게 취할지 알고 있는 어떤 객체를 두자는 것

Page 19: IoC and DI Pattern

Using a Service Locator

Page 20: IoC and DI Pattern

Using a Service Locator

Page 21: IoC and DI Pattern

Using a Segregated Interface for the Locator

Separated Interface 적용 – ISP(Interface segregation principle)

Page 22: IoC and DI Pattern

A Dynamic Service Locator

Page 23: IoC and DI Pattern

Using both a locator and injection with Avalon

Avalon은 Service Locator와 Dependency Injection 모두 사용 가능

Page 24: IoC and DI Pattern

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

Page 25: IoC and DI Pattern

Code or configuration files

XML hell-!!

Compile!!

ex) API vs XML

개발자에겐 프로그래밍 언어가 더 익숙.. 둘 다 지원하고, 선택은 사용자에게 맡겨라.

Page 26: IoC and DI Pattern

Annotation

Spring 3.x, Annotation-based configuration

- Example : @Autowired

Page 27: IoC and DI Pattern

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