I. Definition of template method
Template Method pattern:definethe skeleton of an algorithm in an operation,deferring some steps tosubclasses. Template Methodletssubclasses redefine certain steps of an algorithmwithoutchanging the algorithm ' s structure.
Define the skeleton of an algorithm in an operation, defer execution of some steps to subclasses, and the template method allows subclasses to redefine an algorithm's steps without altering the algorithm's structure.
Second, the application of template method scenario
According to the definition of template method, the skeleton of an algorithm in an operation is defined, in fact, a template method is defined in the abstract class, there will be a series of steps in the template method, some of which are methods in the abstract class, it is the common method of all subclasses, and some methods need to be implemented in subclasses. As a result, template methods are typically used in, for different types, they have the same execution steps, and they have some of the same behavior. We can extract the same behavior into the abstract parent class, and the different behaviors are left to the subclass to implement the code reuse. The template method is a code reuse technique based on inheritance implementations. Below we will take an example to understand the use of template methods.
Iii. examples of template methods
Now suppose we need to construct an algorithm for building a house. Building a house involves the following steps:
Build foundations, build pillars, build walls and windows. Assuming that the sequence of these steps can not be reversed, and the construction of the foundation must be built in front of the pillars, walls built windows, the actual construction of the house is actually the basic steps to, then I need to build two types of house, one is a wooden room, a glass room. and the building of foundations and Windows is a common act of two kinds of houses. Then we will use the template method to implement this process.
1 Public Abstract classHousetemplate {2 3 PublicFinalvoidBuildhouse () {4 buildfoundation ();5 buildpillars ();6 buildwalls ();7 buildwindows ();8System. out. println ("House is built.");9 }Ten OnePrivatevoidbuildfoundation () A { -System. out. println ("Build Foundation"); - } thePrivatevoidbuildwindows () - { -System. out. println ("Build Windows"); - } + - protected Abstract voidbuildpillars (); + A protected Abstract voidbuildwalls (); at -
-}
1 Public classGlasshouse extends housetemplate{2 3 @Override4 protected voidBuildpillars () {5System. out. println ("Building Glass Walls");6 7 }8 9 @OverrideTen protected voidBuildwalls () { OneSystem. out. println ("Building pillars with glass coating"); A - } - the}
public class Woodenhouse extends housetemplate{@Overrideprotected void Buildpillars () {System.out.println ("Building Wooden Walls ");} @Overrideprotected void Buildwalls () {System.out.println ("Building pillars with Wood coating");}
1 Public classhousingclient {2 3 Public Static voidMain (String args[])4 {5Housetemplate Housetype =Newwoodenhouse ();6 7 //using Template Method8 housetype.buildhouse ();9System. out. println ("************");Ten OneHousetype =NewGlasshouse (); A - housetype.buildhouse (); - } the -}
The output of the program is as follows:
Build Foundation
Building Wooden Walls
Building Pillars with Wood coating
Build windows
House is built.
************
Build Foundation
Building Glass Walls
Building Pillars with glass coating
Build windows
House is built.
In the abstract parent class we define buildhouse, a template method that defines the skeleton of the algorithm and defers execution of Buildpillars and buildwalls to subclasses. And the template method is defined as final, preventing subclasses from covering to change the behavior of the parent class, thus violating the "open and closed principle".
Iv. Summary of template methods
1) The template method pattern is a behavioral pattern of a class, where there is only an inheritance relationship between classes in its structure diagram, and no object association relationships. 2) Board method mode is based on the inheritance of code reuse Basic Technology, template method pattern structure and usage is also one of the core object-oriented design. In the template method pattern, you can put the same code in the parent class and put different method implementations in different subclasses. 3) In the template method pattern, we need to prepare an abstract class that implements some of the logic in the form of a concrete method, and then declares some abstract methods to let subclasses implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic, which is the intent of the template method pattern. The template method pattern embodies many important ideas of object-oriented, and is a mode with high frequency. 4) In most cases, a subclass invokes a parent class method, but in a template method the parent class calls a method of the subclass, which is often referred to as Hollywood principle,-- Don't call me, I'll call you.
Design mode-Template method