Design mode: Stencil mode (template pattern)-Go

Source: Internet
Author: User

Stencil Mode

Also called a template method pattern, a skeleton of an algorithm is defined in a method, and some steps are deferred to 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

First, create a creative template (abstract) class

 PackageCom.kaishengit.beverage; Public Abstract classBeverage {/*** Brew coffee or tea ... Process*/     Public Final voidCreate () {boilwater ();//Boil the waterBrew ();//brew with boiling water ...Pourincup ();//to put a. Pour into the cupAddcoundiments ();//Add ...    }     Public Abstract voidaddcoundiments ();  Public Abstract voidBrew ();  Public voidBoilwater () {System.out.println ("Boiled Water"); }         Public voidPourincup () {System.out.println ("Pour into the cup"); }}

2> Create a coffee class (Coffee) and tea class, all inheriting beverage abstract class

1. Coffee (Coffee)

 Package Com.kaishengit.beverage;  Public class extends beverage{    @Override    publicvoid  addcoundiments () {        System.out.println ("Add sugar and Milk");    }    @Override    publicvoid  Brew () {        System.out.println ("Coffee with water");}    } 

2. Tea (TEAS)

 Package Com.kaishengit.beverage;  Public class extends beverage{    @Override    publicvoid  addcoundiments () {        System.out.println ("Add Honey");    }    @Override    publicvoid  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 ();  //  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)

 PackageCom.kaishengit.beverage; Public Abstract classBeverage {/*** Brew coffee or tea ... Process*/     Public Final voidCreate () {boilwater ();//Boil the waterBrew ();//brew with boiling water ...Pourincup ();//to put a. Pour into the cupAddcoundiments ();//Add ...Hook ();//Hooks    }    //Null Implementation Method     Public voidHook () {} Public Abstract voidaddcoundiments ();  Public Abstract voidBrew ();  Public voidBoilwater () {System.out.println ("Boiled Water"); }         Public voidPourincup () {System.out.println ("Pour into the cup"); }}

2> if we engage in activities, drink a cup of coffee, modify the coffee (Coffee) class

 PackageCom.kaishengit.beverage; Public classCoffeeextendsbeverage{@Override Public voidaddcoundiments () {System.out.println ("Add sugar and milk"); } @Override Public voidBrew () {System.out.println ("Coffee with water"); }        /*** Hooks*/@Override Public voidHook () {System.out.println ("One more drink."); }}

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)

 PackageCom.kaishengit.beverage; Public Abstract classBeverage {/*** Brew coffee or tea ... Process*/     Public Final voidCreate () {boilwater ();//Boil the waterBrew ();//brew with boiling water ...Pourincup ();//to put a. Pour into the cup//Hooks decide whether to add ingredients        if(Hook ()) {addcoundiments ();//Add ...        }        //Hook ();    }    /*** Add Ingredients by default *@return     */     Public BooleanHook () {return true; }    //Public void Hook () {}         Public Abstract voidaddcoundiments ();  Public Abstract voidBrew ();  Public voidBoilwater () {System.out.println ("Boiled Water"); }         Public voidPourincup () {System.out.println ("Pour into the cup"); }}

2> We modify the coffee class so that it does not add ingredients

 PackageCom.kaishengit.beverage; Public classCoffeeextendsbeverage{@Override Public voidaddcoundiments () {System.out.println ("Add sugar and milk"); } @Override Public voidBrew () {System.out.println ("Coffee with water"); }        /*** Some guests do not like to add ingredients*/@Override Public BooleanHook () {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

Design mode: Stencil mode (template pattern)-Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.