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:
Template pattern example:
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:
·미리보기 | 소스복사·
- Class MainSubject {
- ITaxStrategy taxCalculator = GetStrategy(taxType);
- //strategy is member class.
- taxCalculator.Calculate();
- private GetStrategy(string taxType) {
- if (taxType == "incometax")
- return new IncomeTaxStrategy();
- else if (taxType == "propertytax")
- return new PropertyTaxStrategy();
- }
- }
- Class IncomeTaxStrategy : ITaxStrategy {
- public Calculate() {
- //calculate based on income tax rates.
- }
- }
- Class PropertyTaxStrategy : ITaxStrategy {
- public Calculate() {
- //calculate based on property tax policies.
- }
- }
·미리보기 | 소스복사·
- abstract Class TaxCalculator {
- public CalculateTax() {
- CalculateIncome();
- tax =+ CalculateTax();
- tax =+ CalculateRelief();
- }
- abstract CalculateTax();
- abstract CalculateRelief();
- }
- Class IncomeTaxCalculator : TaxCalculator {
- override CalculateTax() { //calculate income tax. }
- override CalculateRelief() { //calculate personal relief }
- }
- Class PropertyTaxCalculator : TaxCalculator {
- override CalculateTax() { //calculate property tax. }
- override CalculateRelief() { //do nothing; no relief. }
- }
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
23 | 비즈니스 패턴 | 황제낙엽 | 2008.04.10 | 158 |
22 | SOA Service Benefit Pattern | 황제낙엽 | 2008.04.10 | 129 |
21 | 패턴 입문가에게 권하는 책 - Head First Design Patterns (스토리가 있는 패턴 학습법) | 황제낙엽 | 2007.11.25 | 240 |
20 | 디자인 패턴(GoF) 카다로그 | 황제낙엽 | 2007.11.25 | 160 |
19 | 해석자(Interpreter) | 황제낙엽 | 2007.11.25 | 205 |
18 |
책임연쇄(Chain of Responsibility) 패턴
![]() | 황제낙엽 | 2007.11.25 | 205 |
17 |
복합체(Composite) 패턴
![]() | 황제낙엽 | 2007.11.25 | 180 |
16 |
Singleton(싱글턴) 패턴
![]() | 황제낙엽 | 2007.11.25 | 148 |
15 | Prototype(프로토 타입) 패턴 | 황제낙엽 | 2007.11.25 | 242 |
14 | Builder(빌더) 패턴 | 황제낙엽 | 2007.11.25 | 311 |
13 |
Factory Method(팩토리 메소드) 패턴
![]() | 황제낙엽 | 2007.11.25 | 310 |
12 |
Abstract Factory(추상 팩토리) 패턴
![]() | 황제낙엽 | 2007.11.25 | 185 |
11 |
디자인패턴의 개요&기본 개념 정리
![]() | 황제낙엽 | 2007.11.25 | 128 |
10 | 디자인패턴과 리팩토링의 관계 | 황제낙엽 | 2007.11.20 | 209 |
» | Template Method Pattern | 황제낙엽 | 2007.06.29 | 169 |
8 | The Facade Pattern | 황제낙엽 | 2007.05.23 | 131 |
7 | Factory Pattern | 황제낙엽 | 2007.04.21 | 132 |
6 | Decorator Pattern | 황제낙엽 | 2007.04.21 | 133 |
5 |
해드퍼스트 디자인패턴 샘플 예제 모음
![]() | 황제낙엽 | 2007.04.11 | 185 |
4 | Observer Pattern | 황제낙엽 | 2007.04.05 | 139 |