> For the complete documentation index, see [llms.txt](https://blakes-organization.gitbook.io/rainsister/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blakes-organization.gitbook.io/rainsister/design-pattern/template-pattern.md).

# Template Pattern

### 장점

* 중복코드 줄이고 , 코드의 재사용성을 높인다.
* 메인업무를 관리하기 편하다.
* 추상화 된 메서드의 재정의를 통한 클래스 확장에 유리
* 상위 클래스 메서드만으로도 전반적은 기능동작을 이해할수 있다.

### [전략패턴](broken://pages/nJmeWTDlfbjSAvIdba70) 과 비교

전략패턴(Strategy Pattern)과 Template Pattern은 사용 목적으로 구분된다. 전략패턴은 알고리즘에 대한 숨김, 그리고 쉽게 교체,변경 할수 있음. 하지만 Tempate Pattern 은 알고리즘의 특정 단계만 컨트롤하고 싶을경우 사용한다.

### 코드 예시

```java
public abstract class Game {
    abstract void initialize();
    abstract void startPlay();

    abstract void endPlay();

    public final void play(){
        initialize();
        startPlay();
        endPlay();
    }
}
```

```java
public class CarGame extends Game{
    @Override
    void initialize() {
        System.out.println("레이싱게임초기화 진행중...준비하세요!");
    }

    @Override
    void startPlay() {
        System.out.println("레이싱게임 시작!");
    }

    @Override
    void endPlay() {
        System.out.println("레이싱게임 종료!");
    }
}
```

```java
public class FootballGame extends Game{

        @Override
        void initialize() {
            System.out.println("축구게임초기화 진행중...준비하세요!");
        }

        @Override
        void startPlay() {
            System.out.println("축구게임 시작!");
        }

        @Override
        void endPlay() {
            System.out.println("축구게임 종료!");
        }
}
```

```java
public class MainGameTest {
    public static void main(String[] args) {
        Game game = new FootballGame();
        game.play();
        System.out.println();
        game = new CarGame();
        game.play();
    }
}
```

[소스코드](https://github.com/luxury515/design-pattern-demo/tree/master/TemplateMethodPattern)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blakes-organization.gitbook.io/rainsister/design-pattern/template-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
