Design Mode: Template Pattern-conversion, templatepattern
Template Mode
The template method mode defines the skeleton of an algorithm in a method, and delays some steps to the subclass. The template method allows subclass to redefine some steps in the algorithm without changing the algorithm structure.
Examples of using brewed coffee and brewed tea
Processing Process:
Coffee brewing method: 1. boiling water, 2. brewing coffee with boiling water, 3. pouring coffee into a cup, 4. Adding sugar and milk
Tea brewing method: 1. boil water, 2. Use boiling water to brew tea, 3. Pour tea into a cup, 4. Add honey
1. Create a template (abstract) Class
Package com. kaishengit. beverage; public abstract class Beverage {/*** brewed coffee or tea... process */public final void create () {boilWater (); // boil the water to brew (); // brew with boiling water... pourInCup (); // set... pour the cup addCoundiments (); // Add ...} public abstract void addCoundiments (); public abstract void brew (); public void boilWater () {System. out. println ("boiled water");} public void pourInCup () {System. out. println ("Pour into Cup ");}}
2> creating a Coffee and Tea class inherits the Beverage abstract class.
1. Coffee)
Package com. kaishengit. beverage; public class Coffee extends Beverage {@ Override public void addCoundiments () {System. out. println ("add sugar and milk") ;}@ Override public void brew () {System. out. println ("coffee with water ");}}
2. Tea)
Package com. kaishengit. beverage; public class Tea extends Beverage {@ Override public void addCoundiments () {System. out. println ("add honey") ;}@ Override public void brew () {System. out. println ("tea with water ");}}
3. Let's test:
Package com. kaishengit. beverage; public class Test {public static void main (String [] args) {Coffee coffee = new Coffee (); coffee. create (); // brewed coffee // Tea = new tea (); // brewed Tea // tea. create ();}}
Running result:
-----------------------------------
Boiled Water
Coffee with water
Pour into cup
Add sugar and milk
-----------------------------------
Use hook in template Mode)
There is an empty implementation method. We call this method "hook ". Subclass can decide whether to overwrite it as needed.
1> modify the template class (Beverage)
Package com. kaishengit. beverage; public abstract class Beverage {/*** brewed coffee or tea... process */public final void create () {boilWater (); // boil the water to brew (); // brew with boiling water... pourInCup (); // set... pour the cup addCoundiments (); // Add... hook (); // hook} // empty implementation method public void hook () {} public abstract void addCoundiments (); public abstract void brew (); public void boilWater () {System. out. println ("boiled water");} public void pourInCup () {System. out. println ("Pour into Cup ");}}
2> if we are engaged in activities, get a cup of Coffee and change the Coffee class.
Package com. kaishengit. beverage; public class Coffee extends Beverage {@ Override public void addCoundiments () {System. out. println ("add sugar and milk") ;}@ Override public void brew () {System. out. println ("coffee with water");}/*** hook */@ Override public void hook () {System. out. println ("another cup ");}}
3> use the above test class
Running result:
--------------------------------
Boiled Water
Coffee with water
Pour into cup
Add sugar and milk
Another cup
--------------------------------
"Try again" in the result "...
We can also use the hook to determine whether the code in it is executed.
1> modify the template class (Beverage)
Package com. kaishengit. beverage; public abstract class Beverage {/*** brewed coffee or tea... process */public final void create () {boilWater (); // boil the water to brew (); // brew with boiling water... pourInCup (); // set... pour into the cup // hook to determine whether to add ingredients if (hook () {addCoundiments (); // Add ...} // hook ();}/*** add ingredients by default * @ return */public boolean hook () {return true;} // public void hook () {} public abstract void addCoundiments (); public abstract void brew (); public void boilWater () {System. out. println ("boiled water");} public void pourInCup () {System. out. println ("Pour into Cup ");}}
2> modify the Coffee class so that no ingredients are added.
Package com. kaishengit. beverage; public class Coffee extends Beverage {@ Override public void addCoundiments () {System. out. println ("add sugar and milk") ;}@ Override public void brew () {System. out. println ("coffee with water");}/*** some guests do not like to add ingredients */@ Override public boolean hook () {return false ;} /* @ Override public void hook () {System. out. println ("another cup ");}*/}
3> use the test class above.
Running result:
------------------------------------------------------
Boiled Water
Coffee with water
Pour into cup
------------------------------------------------------
No ingredients are added to the running result.
About template Mode
1> the template mode defines algorithm steps and delays implementation of these steps to subclass
2> the template mode provides us with a code reuse technique.
3> the template abstract class can define specific methods, abstract methods, and hook methods.
4> to prevent the subclass from modifying the algorithm in the template, you can declare the template method as final.
5> A hook is a method. It does not do anything in an abstract class, or only does the default thing. Subclass can choose whether or not to implement it.