Home Template Method Pattern
Post
Cancel

Template Method Pattern

Template Method는 algorithm에서 program skeleton을 정의합니다. 하나 이상의 algorithm 과정들을 subclass에서 그 행동을 재정의합니다. 반면 재정의된 algorithm의 단계를 구성하는 최상위 알고리즘은 그대로 따르게 됩니다. 우선 algorithm의 뼈대를 제공하는 첫 class를 생성합니다.

이 과정에서는 abstract method를 사용해서 algorithm 뼈대를 구현합니다. 이후에 subclass에서 abstract method의 실제 행동을 정의함으로써 그 내용을 변경할 수 있습니다.

즉, 일반적인 algorithm은 한 장소에서 보존하지만, 구체적인 과정은 subclass에서 변경이 될 수 있습니다. 이와 같이 상위 클래스에서 처리의 뼈대를 결정하고, 하위 클래스에서 그 구체적인 내용을 결정하는 디자인 패턴을 Template Method Pattern이라고 한다. 기본적인 Class Diagram Template Method

예제 소스(출처 : Java 언어로 배우는 디자인 패턴 입문. 영진닷컴)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class TemplateMethodPattern {
    public static void main(String[] args) {
        AbstractDisplay d1 = new CharDisplay('H');
       
        AbstractDisplay d2 = new StringDisplay("Hello, Template");
       
        d1.display();
        d2.display();
    }
}

abstract class AbstractDisplay {
    public abstract void open();
    public abstract void print();
    public abstract void close();
    public final void display() {
        open();
        for(int i = 0; i < 5; i++) {
            print();
        }
        close();
    }
}

class CharDisplay extends AbstractDisplay {
    private char ch;
    public CharDisplay(char ch) {
        this.ch = ch;
    }
   
    public void open() {
        System.out.print("<<");
    }
   
    public void print() {
        System.out.print(ch);
    }
   
    public void close() {
        System.out.println(">>");
    }
}

class StringDisplay extends AbstractDisplay {
    private String string;
    private int width;
   
    public StringDisplay(String string) {
        this.string = string;
        this.width = string.getBytes().length;
    }
   
    public void open() {
        printLine();
    }
   
    public void print() {
        System.out.println("|" + string + "|");
    }
   
    public void close() {
        printLine();
    }
   
    public void printLine() {
        System.out.print("+");
        for(int i = 0; i < width; i++) {
            System.out.print("-");
        }
       
        System.out.println("+");
    }
}
This post is licensed under CC BY 4.0 by the author.

Adapter Pattern

Factory Method Pattern