The 1> template pattern defines the steps of the algorithm, extending the implementation of these steps to subclasses
2> template mode provides us with a code reuse technique.
3> Template Abstract classes can define specific methods, abstract methods, and Hook methods
4> to prevent subclasses from changing the algorithm in the template, the template method can be declared as final
A 5> hook is a method that does not work in an abstract class, or only does the default, and subclasses can choose to implement it
Template method pattern, which defines the skeleton of an algorithm in one method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the attaining of the algorithm structure.
Examples of our use of brewed coffee and brewing tea
Processing Flow:
Coffee Brewing Method: 1. Boil the water, 2. Brew coffee in boiling, 3. Pour the coffee into the cup, 4. Add sugar and milk
Tea Brewing Method: 1. Boil the water, 2. Brew the tea with boiling, 3. Pour the tea into the cup, 4. Add Honey
Practice steps:
1> Create a template (abstract) class: Beverage (beverage)
Java code
- Package com.kaishengit.beverage;
- Public abstract class Beverage {
- /**
- * Brew coffee or tea ... Process
- */
- public final void Create () {
- Boilwater (); //Boil the water
- Brew (); //Brew with boiling water ...
- Pourincup (); //Put ... Pour into the cup
- Addcoundiments (); //Plus ...
- }
- 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> Create a coffee class (Coffee) and tea class, all inheriting beverage abstract class
1. Coffee (Coffee)
Java code
- 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 ("Brew Coffee with water");
- }
- }
2. Tea (TEAS)
Java code
- 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:
Java code
- Package com.kaishengit.beverage;
- Public class Test {
- public static void Main (string[] args) {
- Coffee Coffee = new Coffee ();
- Coffee.create (); //Brew coffee
- //tea tea = new tea ();//Brewing Tea
- //tea.create ();
- }
- }
Operation Result:
-----------------------------------
Boiled boiled water
Coffee with water
Pour into the cup
Add sugar and milk
-----------------------------------
Using hooks in template mode
There is an empty implementation method, we call this method "hook". Subclasses can decide whether to overwrite it, depending on the situation.
1> we modify the template class (beverage)
Java code
- Package com.kaishengit.beverage;
- Public abstract class Beverage {
- /**
- * Brew coffee or tea ... Process
- */
- public final void Create () {
- Boilwater (); //Boil the water
- Brew (); //Brew with boiling water ...
- Pourincup (); //Put ... Pour into the cup
- Addcoundiments (); //Plus ...
- Hook (); //Hooks
- }
- //Null 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 engage in activities, drink a cup of coffee, modify the coffee (Coffee) class
Java code
- 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 ("Brew Coffee with water");
- }
- /**
- * Hooks
- */
- @Override
- public void Hook () {
- System.out.println ("one more Cup");
- }
- }
3> using the test class above
Operation Result:
--------------------------------
Boiled boiled water
Coffee with water
Pour into the cup
Add sugar and milk
Another drink.
--------------------------------
The result is "one more drink" ...
We can also use hooks like this to determine if the code inside is executing
1> we modify the template class (beverage)
Java code
- Package com.kaishengit.beverage;
- Public abstract class Beverage {
- /**
- * Brew coffee or tea ... Process
- */
- public final void Create () {
- Boilwater (); //Boil the water
- Brew (); //Brew with boiling water ...
- Pourincup (); //Put ... Pour into the cup
- //Hooks decide whether to add ingredients
- if (Hook ()) {
- Addcoundiments (); //Plus ...
- }
- //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> We modify the coffee class so that it does not add ingredients
Java code
- 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 ("Brew Coffee with water");
- }
- /**
- * Some guests do not like to add ingredients
- */
- @Override
- Public Boolean hook () {
- return false;
- }
- / * @Override
- public void Hook () {
- System.out.println ("One more Cup");
- }
- */
- }
3> or use the test class above
Operation Result:
------------------------------------------------------
Boiled boiled water
Coffee with water
Pour into the cup
------------------------------------------------------
No ingredients added in running results
About template mode
The 1> template pattern defines the steps of the algorithm, delaying the implementation of these steps to subclasses
2> template mode provides us with a code reuse technique.
3> Template Abstract classes can define specific methods, abstract methods, and Hook methods
4> to prevent subclasses from changing the algorithm in the template, the template method can be declared as final
A 5> hook is a method that does not work in an abstract class, or only does the default, and subclasses can choose to implement it
Template Pattern Pattern method