Location>code7788 >text

Design patterns of the template method pattern (three minutes to learn a design pattern)

Popularity:158 ℃/2024-09-07 15:45:58

Template Method Pattern (Template Method Pattern), also called Template Pattern (Template Pattern), is one of the simplest of the design patterns.

Let's look at the definition first:
Defining the skeleton (template) of an algorithm in an operation defers a number of steps to subclasses, and the template method allows subclasses to redefine particular steps of the algorithm without changing the structure of the algorithm.
The definition is still a bit obscure, and here's how I understand it: (anti-theft link: this article first appeared from /jilodream/ )
In the parent class we can define the overall implementation process of a piece of business, but for the specific implementation logic of certain steps, we can define only an abstract method for the time being, and implement/override the method in the future definition of the subclass.
This pattern is mainly to solve, many scenarios, we do not know the future of the actual use of the specific needs of how to implement, and even multiple specific implementation, for this, we can first define the parent class has been clear business.
The rough call structure is as follows:

It is one of the 23 object-oriented design patterns and belongs to the scope of behavioral patterns.
Take a look at the sample code:

Music Player Abstract Class

 1 package ;
 2 
 3 import .slf4j.Slf4j;
 4 
 5 /**
 6  * @discription
 7  */
 8 @Slf4j
 9 public abstract class AbstractMusicPlayer {
10     public void startUp() {
11         showFrame();
12         doCustomizedOpt();
13     }
14 
15     protected abstract void playWelcomeMsg();
16 
17     protected void showFrame() {
18         showMainFrame();
19         playWelcomeMsg();
20     }
21 
22     protected abstract void doCustomizedOpt();
23 
24 
25     protected abstract void showMainFrame();
26 }

Cool Cat Player

 1 package ;
 2 
 3 import .slf4j.Slf4j;
 4 
 5 /**
 6  * @discription
 7  */
 8 @Slf4j
 9 public class CoolCatPlayer extends AbstractMusicPlayer{
10     @Override
11     protected void playWelcomeMsg() {
12       ("hi man");
13     }
14 
15     @Override
16     protected void doCustomizedOpt() {
17         ("You have a free gift worth $99 waiting to be claimed, click the link below");
18     }
19 
20     @Override
21     protected void showMainFrame() {
22         ("Open the main screen of Cool Cat Music.");
23     }
24 }

Kodak Music Box

 1 package ;
 2 
 3 import .slf4j.Slf4j;
 4 
 5 /**
 6  * @discription
 7  */
 8 @Slf4j
 9 public class CoolHePlayer extends AbstractMusicPlayer {
10     @Override
11     protected void playWelcomeMsg() {
12         ("Welcome to Kuta Music Box.");
13     }
14 
15     @Override
16     protected void doCustomizedOpt() {
17         ("A knife 999, stand with your brother and have a good time.");
18     }
19 
20     @Override
21     protected void showMainFrame() {
22         ("Open the main screen of Kuta Music Box.");
23     }
24 }

Execution Master Class

 1 package ;
 2 
 3 /**
 4  * @discription
 5  */
 6 
 7 public class PatternMain {
 8     public static void main(String[] args) {
 9         AbstractMusicPlayer coolCat = new CoolCatPlayer();
10         ();
11 
12         AbstractMusicPlayer coolHe = new CoolHePlayer();
13         ();
14     }
15 }

The output is as follows

15:38:12.515 [main] WARN  - Open the main interface of Coolmusic
15:38:12.518 [main] WARN  - hi man
15:38:12.518 [main] WARN  - You have a free gift valued at $99 waiting to be claimed, click the link below!
15:38:12.518 [main] WARN  - Open the main interface of Kutar Music Box
15:38:12.518 [main] WARN  - Welcome to Kuta Music Box.
15:38:12.518 [main] WARN  - stab999,Stand with your brothers and have a good time.

We define a class for the player and agree on the specific business we need to do when the player starts:

class diagram

Step 1: Open the main interface

Step 2: Do some customized user actions
However, there are multiple players (such as Cool Cat Music, Cool Music Box), their main interface and user-customized operations are different, so we can define only the abstract methods of the above operations, and leave the implementation of specific operations to subclasses to complete.

The steps at the heart of the template approach are 2 points:
A skeleton (template) is defined in the parent class to organize the various defined abstract methods
Subclasses implement abstract methods based on actual business