Github source address
An overview of 23 design patterns
- Java Language Design – An overview of 23 design patterns
Creation pattern
- Factory Method Pattern
- Abstract Factory Pattern
- Builder Mode
- Prototype mode
- Singleton
Structural mode
- Facade Pattern
- Adapter mode (Adapter)
- Proxy mode
- Composite mode
- Flyweight Mode
- Decorator pattern
- Bridge mode (Bridge)
Behavioral pattern
- Mediator Mode
- Observer Model
- Command mode
- Iterator pattern (Iterator)
- Template Method
- Strategy Pattern
- State mode
- Memento Mode
- Interpreter mode
- Chain of Responsibility model
- Visitor Pattern
define
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The template approach allows subclasses to redefine specific steps of an algorithm without changing the structure of that algorithm.
The advantages and disadvantages
Advantages: 1. Encapsulate the invariant part and expand the variable part. 2. Extract common code for easy maintenance. 3. The behavior is controlled by the parent class and implemented by the subclass.
Disadvantages: Each different implementation requires a subclass to implement, resulting in an increase in the number of classes, making the system larger.
Usage scenarios
1. Multiple subclasses share the same methods and logic. 2. Important, complex methods can be considered as template methods.
implementation
We will create a Game abstract class that defines the actions, where the template method is set to final so that it cannot be overridden. Cricket and Football are entity classes that extend Game by overwriting the methods of the abstract class.
TemplatePatternDemo, our demo class uses Game to demonstrate the use of template patterns.
Step 1
Create an abstract class whose template methods are set to final.
Game.java
public abstract class Game {
abstract void initialize(a);
abstract void startPlay(a);
abstract void endPlay(a);
/ / template
public final void play(a) {
// Initialize the game
initialize();
// Start the game
startPlay();
// End the gameendPlay(); }}Copy the code
Step 2
Create an entity class that extends the above classes.
BasketBall.java
public class BasketBall extends Game {
@Override
void endPlay(a) {
System.out.println("BasketBall Game Finished!");
}
@Override
void initialize(a) {
System.out.println("BasketBall Game Initialized! Start playing.");
}
@Override
void startPlay(a) {
System.out.println("BasketBall Game Started. Enjoy the game!"); }}Copy the code
Football.java
public class Football extends Game {
@Override
void endPlay(a) {
System.out.println("Football Game Finished!");
}
@Override
void initialize(a) {
System.out.println("Football Game Initialized! Start playing.");
}
@Override
void startPlay(a) {
System.out.println("Football Game Started. Enjoy the game!"); }}Copy the code
Step 3
Use Game’s template method Play () to demonstrate how the Game is defined.
TemplatePatternDemo.java
public class TemplatePatternDemo {
public static void main(String[] args) {
Game game = new BasketBall();
game.play();
System.out.println();
game = newFootball(); game.play(); }}Copy the code
Step 4
Execute the program and output the result:
BasketBall Game Initialized! Start playing.
BasketBall Game Started. Enjoy the game!
BasketBall Game Finished!
Football Game Initialized! Start playing.
Football Game Started. Enjoy the game!
Football Game Finished!
Copy the code