Template design patterns are mainly derived from the life of some things are template can be followed. Give two examples of life, such as tea and coffee, and take a look.
Tea has the following four steps: 1, boiled water; 2 Put the tea in the Water Cup; 3, pour boiling water; 4, add sugar.
Brewed coffee also has the following four steps: 1, boil water; 2 Put the coffee in the Water Cup; 3, pour boiling water; 4, add sugar and milk.
At this point, it can be clearly found that whether it is tea or coffee, their steps are almost the same: 1, boiling water, 2 into a cup, 3 poured into boiling, 4 plus flavoring agent. Only when specific to a certain drink, they have some steps to be different, tea must be to put tea in the cup, and coffee is to put coffee in the water Cup. At this point we can extract the same steps, create a template, to share, and to the specific content, they have the implementation of the. When specific to the code, the template can be implemented with abstract classes, the specific content can be implemented in its subclasses.
Now use the abstract class to implement the template code,
//abstract base class, which provides a template for all sub-classes Public Abstract classBeverage {//specific template method, to be decorated with the final keyword, to avoid sub-class modification Public Final voidpreparebeveragetemplate () {//1, boil boiling waterBoilwater (); //2, put it in a cup .Putintocup (); //Add boiling waterAddhotwater (); //Add the seasoning due agentaddcondiments (); } Private voidBoilwater () {System.out.println ("Boil water"); } protected Abstract voidPutintocup (); Private voidAddhotwater () {System.out.println ("Add Water"); } protected Abstract voidaddcondiments ();}
As you can see here, in the template, we implemented two methods Boilwater and Addhotwater, because these two methods are the same for any beverage. The other two methods are abstract methods, because only to specific bubble what drink, these two methods, can be specifically realized, here we do not know whether tea or coffee, so there is no way to determine which beverage to put.
The implementation of the abstract method is to go to the subclass, declaring that the coffee subclass and the tea sub-class implement both methods
public class Coffee extends beverage {@Override protected void Putintocup () {System.out. println ( "Put the coffee in the Water Cup" protected void Addcondiments () {System.out.println ( "Add sugar and Milk"
Public class extends Beverage { @Override protectedvoid Putintocup () { System.out.println ( "Put the coffee in the Water Cup"); @Override protectedvoid addcondiments () { System.out.println (" Added sugar and milk ");} }
Now set up a test class testing to use the template mode
Public class Test { publicstaticvoid main (string[] args) { // Coffee making New Coffee (); Coffee.preparebeveragetemplate (); Call Template System.out.println ("\n***********************\n"); // Tea Making New Tea (); Call Template
} }
Java Design Patterns------Template Design Patterns