디자인패턴 Template Method Pattern

황제낙엽 2007.06.29 15:14 조회 수 : 367 추천:104

1. 정의

알고리즘의 일부 단계를 구현하는 것을 서브클래스에서 처리한다.


2. 템플릿 메소드 와 스트레이트지 패턴의 차이


템플릿 메소드는 알고리즘의 일부를 서브클래스 또는 다른 클래스에 구현하지만, 스트레이트지 패턴은 추가적인 행위(알고리즘)에 대해 구성을 통해 확장을 한다는 개념이다. 
즉, 템플릿 메소드는 알고리즘의 완성을 위해 확장되는 서브클래스 또는 다른 클래스의 코드가 필요하지만, 스트레이트지 패턴에서 확장되는 클래스의 코드는 있어도 그만 없어도 그만이다.


3. 다음은 참고문서
(
http://cyrusnet.blogspot.com/2005/07/template-vs-strategy-patterns.html)

Friday, July 29, 2005

Template vs Strategy Patterns

The Template pattern is similar to the Strategy pattern. These two patterns differ in scope and in methodology.

Strategy is used to allow callers to vary an entire algorithm, like how to calculate different types of tax, while Template Method is used to vary steps in an algorithm. Because of this, Strategy is more coarsely grained. The Template allows finer-grained controls in the sequent of operations, and yet allows the implementations of these details to vary.

The other main difference is that Strategy uses delegation while Template Method uses inheritance. In Strategy, the algorithm is delegated to the another xxxStrategy class that the subject will have a reference to, but with Template you subclass the base and override methods to make changes.

Strategy pattern example:
·미리보기 | 소스복사·
  1. Class MainSubject {   
  2.   
  3.     ITaxStrategy taxCalculator = GetStrategy(taxType);    
  4.     //strategy is member class.   
  5.     taxCalculator.Calculate();   
  6.   
  7.     private GetStrategy(string taxType) {   
  8.   
  9.         if (taxType == "incometax")   
  10.             return new IncomeTaxStrategy();   
  11.         else if (taxType == "propertytax")   
  12.             return new PropertyTaxStrategy();   
  13.     }   
  14.   
  15. }   
  16.   
  17. Class IncomeTaxStrategy : ITaxStrategy {   
  18.   
  19.     public Calculate() {   
  20.         //calculate based on income tax rates.   
  21.     }   
  22.   
  23. }   
  24.   
  25. Class PropertyTaxStrategy : ITaxStrategy {   
  26.   
  27.     public Calculate() {   
  28.         //calculate based on property tax policies.   
  29.     }    
  30.   
  31. }  
Template pattern example:
·미리보기 | 소스복사·
  1. abstract Class TaxCalculator {   
  2.   
  3.     public CalculateTax() {   
  4.   
  5.         CalculateIncome();   
  6.         tax =+ CalculateTax();   
  7.         tax =+ CalculateRelief();   
  8.   
  9.     }   
  10.   
  11.     abstract CalculateTax();   
  12.     abstract CalculateRelief();   
  13.   
  14. }   
  15.   
  16. Class IncomeTaxCalculator : TaxCalculator {    
  17.   
  18.     override CalculateTax() { //calculate income tax. }   
  19.     override CalculateRelief() { //calculate personal relief }   
  20.   
  21. }   
  22.   
  23. Class PropertyTaxCalculator : TaxCalculator {    
  24.   
  25.     override CalculateTax() { //calculate property tax. }   
  26.     override CalculateRelief() { //do nothing; no relief. }   
  27.   
  28. }  
번호 제목 글쓴이 날짜 조회 수
64 [ChatGPT] 소프트웨어 품질 관리에서 필요로 하는 소양 황제낙엽 2025.05.02 242
63 [Selenium] 크롬 브라우저 실행 화면을 출력하거나 감추는 방법 황제낙엽 2024.05.05 377
62 [Selenium] 페이지 이동 함수 황제낙엽 2024.05.05 346
61 [Selenium] Chrome 실행 자동화 file 황제낙엽 2024.05.03 1498
60 [Selenium] MS Edge 실행 자동화(RPA, Robotic Process Automation) file 황제낙엽 2024.05.02 701
59 [Selenium] Warning : Unable to find CDP implementation matching 124 황제낙엽 2024.05.02 1062
58 프로젝트 WBS 예제 (Microsoft Project 2013) secret 황제낙엽 2021.05.12 0
57 WBS (Work Breakdown Structure) 황제낙엽 2020.06.18 286
56 프로젝트 진행 절차 file 황제낙엽 2020.05.21 405
55 BDD (Behaviour-Driven Development) 황제낙엽 2020.04.02 358
54 요구사항 개발과 요구사항 관리 요약(요구사항기술서 샘플) file 황제낙엽 2019.07.07 425
53 뒤늦게 정리하는 UML (Unified Modeling Language) 요약 file 황제낙엽 2017.03.29 767
52 StarUML™의 숨은 재미있는 기능 황제낙엽 2017.03.29 2481
51 The Facade Pattern file 황제낙엽 2017.01.04 655
50 와블스 프로세스(Warbles Process) 황제낙엽 2016.07.17 550
49 비 개발자 출신이 스크럼 마스터가 될 수 있는가? 황제낙엽 2015.06.25 621
48 디자인패턴[Observer 패턴] 황제낙엽 2013.10.18 500
47 PMBOK 5판 이해관계자 관리의 이해 [1] file 황제낙엽 2013.08.23 1650
46 Validation and Verification file 황제낙엽 2013.08.23 432
45 이해관계자의 관리/이해관계자 분석 황제낙엽 2013.08.23 670