#MOC
[[🗃️デザインパターン]]の1つ。
スーパークラスで処理の枠組みを定めて、サブクラスで具体的な処理内容を定義する実装方法。
## 例
- 料理のスーパークラスと、パスタ、カレーの料理手順をサブクラスにまとめているパターン。
```mermaid
classDiagram
Meal <|.. PastaMeal
Meal <|.. CurryMeal
class Meal{
+void doMeal()
+void serve()
+void cleanUp()
-void prepareIngredients()
-void cook()
}
class PastaMeal{
+void prepareIngredients()
+void cook()
}
class CurryMeal{
+void prepareIngredients()
+void cook()
}
```
```php
abstract class Meal {
public function doMeal() {
$this->prepareIngredients();
$this->cook();
$this->serve();
}
abstract public function prepareIngredients();
abstract public function cook();
public function serve() {
echo "Serving the dish.\n";
}
}
class PastaMeal extends Meal {
public function prepareIngredients() {
echo "Preparing pasta, garlic, and tomato sauce.\n";
}
public function cook() {
echo "Cooking pasta in hot water and then adding sauce.\n";
}
}
class CurryMeal extends Meal {
public function prepareIngredients() {
echo "Preparing rice, chicken, and curry sauce.\n";
}
public function cook() {
echo "Boiling rice; frying chicken then adding curry sauce.\n";
}
}
```
## 📚ドキュメント
- [Template Method パターン - Wikipedia](https://ja.wikipedia.org/wiki/Template_Method_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3)
- [3.TemplateMethod パターン | TECHSCORE(テックスコア)](https://www.techscore.com/tech/DesignPattern/TemplateMethod)
## 📖ノウハウ
## 💁トラブルシューティング