티스토리 뷰
Template Method Pattern
- 추상클레스에서 전체 비지니스 로직이 템플릿 형태로 존재하고, 특정 디테일 메소드를 추상 메소드로 선언하여 상속받은 각 클레스에서 처리한다.
- 전체적인 로직은 비슷하지만, 일부 로직만 틀릴 경우에 사용한다.
public class SubtleMethod extends StealingMethod {
@Override
protected String pickTarget() {
return "shop keeper";
}
@Override
protected void confuseTarget(String target) {
System.out.println("Approach the {} with tears running and hug him!", target);
}
@Override
protected void stealTheItem(String target) {
System.out.println("While in close contact grab the {}'s wallet.", target);
}
}
public class HitAndRunMethod extends StealingMethod {
@Override
protected String pickTarget() {
return "old goblin woman";
}
@Override
protected void confuseTarget(String target) {
System.out.println("Approach the {} from behind.", target);
}
@Override
protected void stealTheItem(String target) {
System.out.println("Grab the handbag and run away fast!");
}
}
public abstract class StealingMethod {
protected abstract String pickTarget();
protected abstract void confuseTarget(String target);
protected abstract void stealTheItem(String target);
public void steal() {
String target = pickTarget();
System.out.println("The target has been chosen as {}.", target);
confuseTarget(target);
stealTheItem(target);
}
}
public class HalflingThief {
private StealingMethod method;
public HalflingThief(StealingMethod method) {
this.method = method;
}
public void steal() {
method.steal();
}
public void changeMethod(StealingMethod method) {
this.method = method;
}
}
public class App {
public static void main(String[] args) {
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
thief.steal();
thief.changeMethod(new SubtleMethod());
thief.steal();
}
}
'Programming > 디자인 패턴' 카테고리의 다른 글
Observer Pattern (0) | 2019.01.24 |
---|---|
Factory Method Pattern (0) | 2019.01.24 |
Builder Pattern (0) | 2019.01.24 |
Strategy Pattern (0) | 2019.01.24 |
- Total
- Today
- Yesterday
- JPA Criteria
- Mapping
- Discriminate Mapping
- java Equals
- JPA
- Criteria
- docker
- Query DSL
- Embedded Mapping
- @Primary
- Registrar
- Embeddable Mapping
- Join Table
- java EqualsAndHashCode
- RetryTemplate
- Typesafe Config
- Spring
- 복합키 Mapping
- Spring JDBC Template
- spring spel
- Akka
- Charles proxy
- Spring Registrar
- SmartLifecycle
- DI
- guava
- java generic
- Property
- Sprint RetryTemplate
- scikit-learn
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |