Original address: http://www.cnblogs.com/jenkinschan/p/5768760.html , overview
The template method pattern defines the skeleton of an algorithm in one 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 structure of the algorithm. second, structure class diagram
third, solve the problem
Template method is to provide an algorithm framework, the framework of the steps are some of the parent class has been set, and some need to implement the subclass themselves. The process is set up to do one thing, but some steps need to be done yourself, and there are steps that others can do for us. To take the site, the general procedure is to purchase a domain name –> purchase space –> upload the site –> record –> Audit, each site must be created through such a fixed procedure, but in addition to the audit does not need to build station care, the other steps are to build the station himself to complete. Iv. Examples of applications
Now many of our families have soya-bean milk machine, the nutritional value of soy milk does not need me to say. The process of making soy milk simple point is to add ingredients--------------to the soy milk machine to break, by adding different ingredients, you can make different flavors of soy milk, but material selection, immersion and put into the soy milk machine smash These steps for the production of each flavor of soy milk are the same.
1. Creating abstract Classes
Package Templatemethod.pattern;
Soya-bean milk, abstract class public abstracts classes
Soyamilk {
//This is a template method, with final decoration, does not allow subclasses to overwrite. The template method defines the procedure for making soy milk
final void preparerecipe () {
selectmaterial ();
Addcondiments ();
Soak ();
Beat ();
}
Selection method, the choice of soy
void selectmaterial () {
System.out.println ("The first step, the selection of fresh soybeans");
}
You can add different toppings, which are set here as abstract methods, and subclasses must implement the abstract
void addcondiments ();
Soak
void Soak () {
System.out.println ("step three, soybeans and ingredients to soak, about 5 hours");
}
Put the soy milk machine smashed
void Beat () {
System.out.println ("Fourth step, soy ingredients put into soy milk machine smash");}
}
2, the creation of red JuJube milk
Package Templatemethod.pattern;
Red JuJube soy Milk public
class Reddatessoyamilk extends soyamilk{
//Implementation of the parent class add batching method
@Override
void Addcondiments () {
System.out.println ("Second step, add JuJube ingredient");
}
}
3. Create Walnut soy milk
Package Templatemethod.pattern;
Walnut soy Milk Public
class Nutsoyamilk extends soyamilk{
//Implement Add batching method for parent class
@Override
void addcondiments () {
System.out.println ("second step, adding walnut ingredients");
}
}
4, test making soy milk
Package Templatemethod.pattern;
public class Soyamilktest {public
static void Main (string[] args) {
//make JuJube soy milk
System.out.println ();
SYSTEM.OUT.PRINTLN ("-----making jujube soy milk step-------");
Soyamilk reddatessoyamilk = new Reddatessoyamilk ();
Reddatessoyamilk.preparerecipe ();
Making walnut soy Milk
System.out.println ();
SYSTEM.OUT.PRINTLN ("-----Making walnut soy milk step-------");
Soyamilk nutsoyamilk = new Nutsoyamilk ();
Nutsoyamilk.preparerecipe ();
}
}
v. Advantages and Disadvantages
1. Advantages
(1), the algorithm exists only in one place, that is, in the parent class, easy to modify. When you need to modify an algorithm, subclasses inherit these modifications as long as you modify the template method of the parent class or some steps that have already been implemented.
(2), maximize code reuse is realized. The template method of the parent class and some of the steps that have been implemented are used directly by the quilt class inheritance.
(3), the unified algorithm, but also provides a lot of flexibility. The template method of the parent class ensures that the structure of the algorithm remains the same, while the subclass provides a partial step implementation.
(4), provides a basic framework, easy to expand subclasses. The template method has a framework that controls how to do things, while the person using the framework specifies the details of each step in the framework algorithm. Subclasses can use the parent class's algorithm as long as they inherit the parent class and implement the abstract method.
2. Disadvantages
(1), template methods using inheritance to reuse code, if you want to add a step in the basic algorithm, and the step is abstract, each subclass to modify the code to achieve this step. six, the method of the hook in the template method
In the parent class of the template method pattern, we can define a method that does nothing by default, which is called a "hook", as the case may not overwrite it. We still use the above to make soy milk example to explain.
1. Create a parent class with a hook method
Package Templatemethod.pattern;
Soya-bean milk, abstract class public abstracts classes
Soyamilkwithhook {
//This is a template method, with final decoration, does not allow subclasses to overwrite. The template method defines the procedure for making soy milk
final void preparerecipe () {
selectmaterial ();
Determines whether to add the batching
if (customerwantscondiments ()) {
addcondiments ();
}
Soak ();
Beat ();
}
Selection method, the choice of soy
void selectmaterial () {
System.out.println ("Selected fresh Soybeans");
}
You can add different toppings, which are set here as abstract methods, and subclasses must implement the abstract
void addcondiments ();
Soak
void Soak () {
System.out.println ("The material begins to soak, it takes about 5 hours");
}
Put the soy milk machine smashed
void Beat () {
System.out.println ("Material put into the soy milk machine to smash");
}
Hook method, whether to add batching
Boolean customerwantscondiments () {
return true;
}
}
2, create pure soy milk sub-category
Package Templatemethod.pattern;
Make pure soy milk, do not add any ingredients public
class Puresoyamilk extends soyamilkwithhook{
@Override
void addcondiments () {
}
Override hook method, do not add batching
@Override
boolean customerwantscondiments () {
return false;
}
}
3, test production of pure soy milk
Package Templatemethod.pattern;
public class Puresoyamilktest {public
static void Main (string[] args) {
//make pure soy milk
System.out.println (); C15/>SYSTEM.OUT.PRINTLN ("-----Making pure soy milk step-------");
Soyamilkwithhook puresoyamilk = new Puresoyamilk ();
Puresoyamilk.preparerecipe ();
}
}
The role of the hook method
1, let the sub-class implementation of the optional part of the algorithm. Some of the steps in the algorithm are optional, and subclasses can make decisions about whether these steps are required.
2, if the hook for the implementation of the subclass is not important, the subclass can ignore the hook.
3. Hooks can allow subclasses to react to certain upcoming (or just occurring) steps in a template method. We can implement the action that we need to make for a step in the hook, call the hook when a step of the template method executes.