Template Method design pattern behavior ------
Define a framework of operation of the algorithm, some steps to subclasses delay. Subclasses that may not change the structure of certain steps of an algorithm of the algorithm to redefine.
Method pattern template (Template Method Pattern) actually encapsulates a fixing process that consists of several steps, the specific steps may be implemented by a different subclass, so that the fixing process to produce different results.
Template Method pattern is very simple, in fact, the class inheritance mechanism, but it is a very wide range of application mode.
Template Method Essence: abstract packaging processes, particularly for realization
Mainly to solve
Upon completion of the operation of a process having a fixed, fixed by the abstract process steps, the specific steps to achieve specific subclasses (fixed process, different implementations).
Advantages and disadvantages
advantage
- Package constant, variable expansion : the parent class encapsulates the process and achieve some specific behavioral change, the behavior of other variable subclasses referred embodied;
- Controlled by the parent process, subclasses implement : a frame defined by the parent process, subclass can not be changed; subclasses can be embodied for certain steps in the process;
Shortcoming
- Abstract provisions of the act, specifically responsible for implementation, and behavior contrary to usual things, will bring difficulties in understanding (In layman's terms, "the father calls the subclass method");
scenes to be used
- A plurality of sub-class has public methods, and the logic is substantially the same;
- Important, complex algorithms, the core algorithm can be designed as a template method, functional details around by each sub-class implementation;
- When reconstituted, the template model method is a model frequently used, the same code to extract parent class, then constrain behavior hook function;
UML 图
From the UML class diagram, we can see that the template method pattern mainly includes two roles:
- Abstract template (the AbstractClass) : abstract template class, the framework defines a set of algorithms / procedures;
- Embodied (ConcreteClass) : implementation class, some of the steps of the algorithm framework / process were implemented;
/ ** * * abstract template class: defines cooking process * / public abstract class CookVegetable { protected void Wash () { System.out.println ( "vegetables" ); } protected void pourOil () { System.out.println ( "hot oil pot" ); } protected void Fry () { System.out.println ( "the fry dishes" ); } // specifically determined by the food seasoning protected abstract void pourSauce (); // specific cooking process public Final void cook() { this.wash(); this.pourOil(); this.fry(); this.pourSauce(); System.out.println("起锅吃菜"); } }
/ ** * fried bean sprouts * * / public class CookBeanSprout the extends CookVegetable { @Override protected void pourOil () { System.out.println ( "less oil pot" ); } @Override protected void Fry () { the System.out. the println ( "stir quickly" ); } @Override protected void pourSauce () { System.out.println ( "salt soy sauce and a small amount" ); } }
/ ** * fried eggplant * * / public class CookEggplant the extends CookVegetable { @Override protected void Wash () { System.out.println ( "head and tail removed, and then the water" ); } @Override protected void pourOil () { System.out.println ( "more oil pot" ); } @Override protected void pourSauce () { System.out.println ( "salt and chicken" ); } }
run
public class Client { public static void main (String [] args) { System.out.println ( "fried bean sprouts ready" ); CookVegetable cookVegetable = new new CookBeanSprout (); cookVegetable.cook (); System.out.println (); System.out.println ( "prepare fried eggplant" ); cookVegetable = new new CookEggplant (); cookVegetable.cook (); } }