[JAVA]/[자바 기초]
[자바 기초] 템플릿 메서드 패턴 (Template Method Pattern)
북방바다코끼리표범
2023. 8. 10. 12:48
복습
https://shins99.tistory.com/35
[자바 기초] 추상 클래스
복습 https://shins99.tistory.com/34 [자바 기초] 다형성 복습 https://shins99.tistory.com/32 [JAVA] 상속 복습 https://shins99.tistory.com/25 [자바 기초] 배열 복습 https://shins99.tistory.com/23 [자바 기초] static 복습 https://shi
shins99.tistory.com
💡 학습 목표
코드의 실행의 흐름 정의하기
템플릿 메서드 패턴 (Template Method Pattern)
- 추상 메서드, 구현된 메서드 활용해서 코드의 흐름(시나리오)를 정의하는 디자인 패턴
- final로 선언 = 메서드 오버라이드 금지
- 프레임 워크에서 자주 사용
- 부모 클래스에서 선언, 전체적인 흐름 정의
public abstract class Car {
public abstract void drive();
public abstract void stop();
public void startCar() {
System.out.println("시동을 켭니다.");
}
public void turnOff() {
System.out.println("시동을 끕니다.");
}
// 후크 메서드
public void washCar() {
}
// 핵심! 템플릿 메서드 패턴
// final 키워드를 활용해서 재정의 할 수 없도록 설계
// 실행의 흐름을 미리 만들어 둔다.
final public void run() {
startCar();
drive();
stop();
turnOff();
}
}
후크 메서드
- 아무것도 구현하지 않는 일반 메서드
- 템플릿 메서드 안에 넣어놓고 필요시 재정의 하여 사용가능 하게 함