[디자인 패턴] Template Method pattern
Template Method is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
상위에서 알고리즘의 뼈대를 정의하고, 하위에서 구체적인 알고리즘을 <structure의 변경 없이> 재정의 하는 패턴
이 패턴을 아래의 예시로 보면,
The Template Method pattern suggests that you break down an algorithm into a series of steps, turn these steps into methods, and put a series of calls to these methods inside a single template method. The steps may either be abstract, or have some default implementation.
하나의 알고리즘(아래에서는 "mine")을 각 단계로 나눈 후, 이를 각각의 메소드로 바꾸어 준다.
바꾼 이 메소드들을 하나의 template method에서 호출한다.
이 과정들(메소드)은 abstract로 명시되어 하위에서 정의될 수도 있고, default로 상위에서 정의될 수도 있다.
- Template Method is based on inheritance: it lets you alter parts of an algorithm by extending those parts in subclasses. Strategy is based on composition: you can alter parts of the object’s behavior by supplying it with different strategies that correspond to that behavior. Template Method works at the class level, so it’s static. Strategy works on the object level, letting you switch behaviors at runtime.
https://refactoring.guru/design-patterns/template-method
Template Method
Problem Imagine that you’re creating a data mining application that analyzes corporate documents. Users feed the app documents in various formats (PDF, DOC, CSV), and it tries to extract meaningful data from these docs in a uniform format. The first vers
refactoring.guru