Template Mode
In template mode (Template Pattern), and an abstract class that defines how to execute it discloses a method / template. Its subclasses need to override the method can achieve, but is called abstract class will be defined. This type of design patterns belong behavioral patterns.
Introduction
Intent: Define a skeleton operation algorithm, while deferring some steps to subclasses.Template Method lets subclasses of an algorithm without changing the structure of a particular step of the algorithm can be redefined.
Main Solution: Some common method, but in each sub-class re-write this approach.
When to use: There aresome common methods.
How to solve: these general algorithm abstracted.
The key code: abstract class implements the other steps in the sub-class implementation.
Application examples: 1, when in a house built, foundations, wiring, water pipes are the same, only in the latter part of the building have a closet plus plus fences differences.2, Journey to the West set a good Buddha inside 81 difficult, this is a top-level logical framework. 3, Spirng in Hibernate support, some have already given a good way to encapsulate, such as open transactions, obtaining Session, Close Session and other programmers who have not repeated write good code norms, direct throw an entity can be saved.
Advantages: 1, part of the same package, extended variable part.2, extract common code, easy to maintain. 3, the behavior is controlled by the parent class, subclass implementation.
Disadvantages: each of the different implementations require a subclass to achieve, resulting in an increase in the number of classes, making the system more substantial.
Usage scenarios: 1, multiple sub-class common method, and the same logic.2, important, complex methods can be considered as a template method.
Note: To prevent malicious actions, coupled with the general template methods are final keywords.
achieve
We will create a custom actionGameabstract class, wherein the template method is set to final, so it will not be overwritten.CricketandFootballGame is an extension of the entity class, which overrides the abstract class.
TemplatePatternDemo,we demonstrate the use ofGameclass to demonstrate the use of a template pattern.
step 1
Create an abstract class, its template method is set to final.
Game.java
public abstract class Game { abstract void initialize (); abstract void startPlay (); abstract void endPlay (); // Template public final void play () { // Initialize the game initialize (); // Start the game startPlay (); // End game endPlay (); } }
Step 2
Create extends the entity classes above class.
Cricket.java
public class Cricket extends Game { @Override void endPlay () { System.out.println ( "Cricket Game Finished!"); } @Override void initialize () { System.out.println ( "! Cricket Game Initialized Start playing."); } @Override void startPlay () { System.out.println ( "Cricket Game Started Enjoy the game.!"); } }
Football.java
public class Football extends Game { @Override void endPlay () { System.out.println ( "Football Game Finished!"); } @Override void initialize () { System.out.println ( "! Football Game Initialized Start playing."); } @Override void startPlay () { System.out.println ( "Football Game Started Enjoy the game.!"); } }
Step 3
Gameusing the template method play () to demonstrate the game are defined.
TemplatePatternDemo.java
public class TemplatePatternDemo { public static void main (String [] args) { Game game = new Cricket (); game.play (); System.out.println (); game = new Football (); game.play (); } }
Step 4
Verify output.
Cricket Game Initialized! Start playing. Cricket Game Started. Enjoy the game! Cricket Game Finished! Football Game Initialized! Start playing. Football Game Started. Enjoy the game! Football Game Finished!