Template method Pattern Definition: Define the skeleton of an algorithm in a method, and defer some steps into the subclass, the template method allows subclasses to redefine some of the steps in the algorithm without altering the algorithm structure.
Design principles: Do not call us, I will call you, (like headhunters and job seekers, job seekers do not need to find a headhunter, headhunting as long as there is a job will find you) called the Hollywood Principle
Citing an example, there are two drinks in a shop: coffee and tea, their brewing methods
The first step: all to use boiling water
Step two: Brew coffee or tea with hot water
Step three: Pour the drink into the cup
Fourth step: Add the appropriate seasoning to the drink
In the above steps one or three is the same, can be extracted out of the base class, two or four different, can be abstracted out, let the subclass to achieve, the following is the beverage abstract parent class, the parent class can let the unknown subclass to do it itself may be done bad or can not complete the things ; You can add a hook function: custcomerwantscondiments (), please subclass to rewrite.
Coffeinebeveragewithhook.java
Package com.dzt.template;/** * Because some customers may not need seasoning, then need to let customers choose whether to add seasoning, need * * @author Administrator * */public Abstract class Coffeinebeveragewithhook {void Preparerecipe () {boilwater (); Brew ();p Ourincup (); if ( Custcomerwantscondiments ()) addcondiments ();} abstract void Brew (); Brewing abstract void addcondiments (); Add seasoning//boiled water void boilwater () {System.out.println ("Coffeinebeveragewithhook-------------------->boilwater");} Pour the drink into the Cup void Pourincup () {System.out.println ("Coffeinebeveragewithhook-------------------->pourincup");} /** * Subclasses can overload this function * * @return */boolean custcomerwantscondiments () {return true;}}
The following is the coffee class, which inherits the drink from above. Parent class
Coffeinewithhook.java
Package Com.dzt.template;public class Coffeinewithhook extends Coffeinebeveragewithhook {@Overridevoid Brew () {//TODO Auto-generated method StubSystem.out.println ("Coffeinewithhook-------------------->brew");} @Overridevoid addcondiments () {//TODO auto-generated method StubSystem.out.println (" Coffeinewithhook-------------------->addcondiments ");} @Overrideboolean custcomerwantscondiments () {//TODO auto-generated method Stubreturn false;}}
Below is the tea class, which inherits the drinks from the parent class
Teawithhook.java
Package Com.dzt.template;public class Teawithhook extends Coffeinebeveragewithhook {@Overridevoid Brew () {//TODO Auto-generated method StubSystem.out.println ("Teawithhook-------------------->brew");} @Overridevoid addcondiments () {//TODO auto-generated method StubSystem.out.println ("Teawithhook-------------------- >addcondiments ");} @Overrideboolean custcomerwantscondiments () {//TODO auto-generated method Stubreturn true;}}
The above code can go to modify the Custcomerwantscondiments () function, the user decides whether to add seasoning
Test code
Package com.dzt.template;/** * Template method mode adds a hook method to the template method, allowing the user to decide * * @author Administrator * @date 2014.08.20 */public class T Emplatemain {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubcoffeinewithhoo K Coffhook = new Coffeinewithhook (); Teawithhook Teahook = new Teawithhook (); Coffhook.preparerecipe (); System.out.println ("---------------------------------------------------------------"); Teahook.preparerecipe ();}}
Summarize:
1, template method is a kind of code reuse basic technology. They are particularly important in class libraries, which extract public behavior from the class library.
2. The template method leads to a directional control structure, the "Hollywood Rule": "Don ' t call Me,i will calls you.", that is, a parent class invokes the operation of a subclass, not the other way around.
3, the type of template invoke operation has specific operation, specific abstracclass operation, primitive operation, factory method, hook operation. The primitive operation is less defined.
4. Template methods use inheritance to change part of the algorithm. The policy pattern uses delegates to change the entire algorithm.
Related code: http://download.csdn.net/detail/deng0zhaotai/7835511
Reference book: "Head First design mode"
Template method mode of Android design mode