KOO-YS / java-design-pattern

0 stars 0 forks source link

[Chapter 03&04] Template Method와 Factory Method #1

Closed KOO-YS closed 3 years ago

KOO-YS commented 3 years ago

Part 2에서 클래스의 상속과 관련된 디자인 패턴이 2가지 소개되었습니다

두 디자인 패턴을 명확하게 구분 지을 수 있도록 보충 설명을 추가

KOO-YS commented 3 years ago

Template Method와 Factory Method 비교 테이블

템플릿 메소드 패턴 팩토리 메소드 패턴
패턴 분류 행위(Behavioral) 패턴 생성(Creational) 패턴
패턴 의도 객체나 클래스 사이의 알고리즘이나 책임 분배에 관련된 패턴. 객체 사이의 결합도 최소화에 중점 객체 생성과 조합을 캡슐화해 객체의 변경이 프로그램에 영향을 받지 않도록 유연성 제공
패턴정의 ◼ 어떤 작업을 처리하는 일부분을 서브 클래스로 캡슐화해 전체 일을 수행
◼ 구조는 바꾸지 않으면서 특정 단계에서 수행하는 내역을 바꾸는 패턴
객체 생성 처리를 서브 클래스로 분리해 처리하도록 캡슐화하는 패턴
◼ 객체 생성을 분리함으로써 변화에 대비
장점 ◼ 전체적으로 동일하면서 부분적으로 다른 구문으로 구성된 메소드의 코드 중복을 최소화
◼ 전체적인 알고리즘은 상위 클래스에서 구현하면서(중복 부분) 다른 부분은 하위 클래스에서 구현할 수 있도록 함
◼ 객체의 생성 코드를 별도의 클래스/메소드로 분리함으로써 객체 생성의 변화에 대비하는데 유용
◼ 동일한 형태로 확장성 있는 프로그래밍 가능



참고 블로그

KOO-YS commented 3 years ago

Template Method와 Factory Method의 활용

Template method

Template Method is used prominently in frameworks. Each framework implements the invariant pieces of a domain's architecture, and defines "placeholders" for all necessary or interesting client customization options. In so doing, the framework becomes the "center of the universe", and the client customizations are simply "the third rock from the sun". This inverted control structure has been affectionately labelled "the Hollywood principle" - "don't call us, we'll call you".

Template method Example

daily routine of a worker


Factory method

People often use Factory Method as the standard way to create objects; but it isn't necessary if: the class that's instantiated never changes, or instantiation takes place in an operation that subclasses can easily override (such as an initialization operation).

Factory method Example

class of toy (car, action figure, etc.) is determined by the mold



참고 사이트