본문 바로가기
Computer Science/디자인패턴

Template Method Pattern

by _S0_H2_ 2022. 10. 25.
728x90
반응형

Template Method Pattern은 알고리즘의 일부 단계를 서브 클래스에서 구현할 수 있으며, 알고리즘의 구조는 그대로 유지하며서 알고리즘의 특정 단계를 서브클래스에서 재정의할 수도 있다.

아래에서 coffee와 tea가 각각
coffee
- prepare
- boil
- brew coffee grind
- pour in cup
- add sugar and milk

tea
- prepare
- boil
- steep tea bag
- pour in cup
- add lemon

의 과정으로 만들어진다면, boil과 pour in cup의 과정은 base에 공통적으로 구현할 수 있다.

추가적으로, brew coffee grind 와 steep tea bag 이 비슷하고 add sugar and milk 와 add lemon이 비슷하 과정이므로 각각 brew와 add condiments 추상메서드로 선언한 뒤 각 메소드는 서브클래스가 처리하도록 할 수 있다.

 

코드는 git!

728x90
반응형

'Computer Science > 디자인패턴' 카테고리의 다른 글

Composite Pattern  (0) 2022.11.15
Iterator Pattern  (0) 2022.11.01
Facade Pattern  (0) 2022.10.18
Adapter Pattern  (0) 2022.10.18
Command Pattern  (0) 2022.09.28