1 Introduction
The idea of the template method pattern is to define the main frame of the program in the abstract class, and implement the specific details in the subclass, which is a behavioral design pattern.
Implementation: There are two roles in the template method pattern, abstract class and implementation class. In the abstract class, the main structure or process of the program is defined, and the method can be set as final, so that it cannot be overridden in subclasses. For other detailed method definitions, it is delayed to the implementation class.
scenes to be used:
- For example, cooking requires a set of main processes such as starting a fire, setting up a pot, and adding ingredients. This can be placed in an abstract class. But how much fire to turn on, what kind of pot to use, and what ingredients to put in, are placed in sub-categories, because each dish is made in different ways.
- such as eating. Always wash your hands, take utensils, and eat. The main process is unified. But what to wash hands with, what utensils to use to eat, and what to eat, there are many situations.
2. Case
Take everyday life and work as an example. The main frame is, eat breakfast, go to work, eat lunch, go to work, eat dinner. The specific implementation details include what to eat for three meals and what to do at work.
2.2. Implementation
abstract class
public abstract class Life { abstract void makeBreakfast(); abstract void makeLunch(); abstract void makeDinner(); abstract void MorningWork(); abstract void AfternoonWork(); public final void live() { makeBreakfast(); MorningWork(); makeLunch(); AfternoonWork(); makeDinner(); } }
Subclass
public class Chairman extends Life { @Override void makeBreakfast() { System.out.println("Breakfast: eat western food"); } @Override void makeLunch() { System.out.println("Lunch: Eat Japanese food"); } @Override void makeDinner() { System.out.println("Dinner: eat the full Chinese feast"); } @Override void MorningWork() { System.out.println("Morning work: dealing with internal company affairs"); } @Override void AfternoonWork() { System.out.println("Afternoon work: dealing with external public relations of the company"); } }
public class Programmer extends Life { @Override void makeBreakfast() { System.out.println("Breakfast: KFC"); } @Override void makeLunch() { System.out.println("Lunch: McDonald's"); } @Override void makeDinner() { System.out.println("Dinner: Dicos"); } @Override void MorningWork() { System.out.println("Morning work: write program"); } @Override void AfternoonWork() { System.out.println("Afternoon work: debugging program"); } }
validator
public class Demo { public static void main(String[] args) { Programmer programmer = new Programmer (); programmer.live(); System.out.println(""); Chairman chairman = new Chairman(); chairman.live(); } }
operation result
Breakfast: KFC
Morning work: Writing programs
Lunch: McDonald's
Afternoon work: debugging programs
Dinner: DicosBreakfast: eat Western food
Morning work: handle internal company affairs
Lunch: eat Japanese food
Afternoon work: handle external public relations
dinner: eat a full-scale banquetProcess finished with exit code 0
3. Summary
Advantages: Extract the unchanged main part into an abstract class as a template method for easy maintenance