디자인패턴 Template Method Pattern

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

sitelink1  
sitelink2  
sitelink3  
extra_vars4  
extra_vars5  
extra_vars6  

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. }