Because of its own defects, the inheritance relationship has been detained by experts as "evil. Experts such as "using delegate relationships instead of inheritance relationships" and "Using Interfaces as much as possible instead of abstract class inheritance" warn us that these cainiao will "look at inheritance differently ". In fact, inheritance still has many advantages. It seems that the disadvantages are even more obvious. The rational use of the inheritance relationship can still play a good role in your system design. The template method mode is an example.
GOF defines the skeleton of an algorithm in an operation for the Template Method mode, and delays some steps to the subclass. This allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. The structure of the algorithm here can be understood as the business process you have designed based on your needs. A specific step refers to the links where variables may exist in the content.
As you can see, the template method is designed to cleverly solve the impact of changes on the system. The template method enhances system scalability, minimizing the impact of changes on the system. This can be clearly seen in the following example.
Copy codeThe Code is as follows:
<? Php
/**
* Template Mode
*
* Defines the algorithm skeleton in an operation and delays some steps to the subclass so that the subclass can define certain steps of the Algorithm without changing the structure of an algorithm.
*
*/
Abstract class TemplateBase
{
Public function Method1 ()
{
Echo "abstract Method1 <br/> ";
}
Public function Method2 ()
{
Echo "abstract Method2 <br/> ";
}
Public function Method3 ()
{
Echo "abstract Method3 <br/> ";
}
Public function doSomeThing ()
{
$ This-> Method1 ();
$ This-> Method2 ();
$ This-> Method3 ();
}
}
Class TemplateObject extends TemplateBase
{
}
Class TemplateObject1 extends TemplateBase
{
Public function Method3 ()
{
Echo "TemplateObject1 Method3 <br/> ";
}
}
Class TemplateObject2 extends TemplateBase
{
Public function Method2 ()
{
Echo "TemplateObject2 Method2 <br/> ";
}
}
// Instantiate
$ ObjTemplate = new TemplateObject ();
$ ObjTemplate1 = new TemplateObject1 ();
$ ObjTemplate2 = new TemplateObject2 ();
$ ObjTemplate-> doSomeThing ();
$ ObjTemplate1-> doSomeThing ();
$ ObjTemplate2-> doSomeThing ();
AbstractClass (abstract class): defines one or more abstract methods for specific subclasses to implement them. It also implements a template method to define the skeleton of an algorithm. This template method not only calls the preceding abstract method, but also calls other operations as long as it can fulfill its mission.
ConcreteClass: implements the abstract methods in the parent class to complete the steps related to the specific subclass in the algorithm.
Based on the definition analysis and examples, we can see that the template method is applicable to the following situations:
Implement the unchanged part of an algorithm at one time, and leave the variable behavior to the subclass for implementation.
Common behaviors in each subclass should be extracted and concentrated into a common parent class to avoid code duplication. In fact, this is a good coding habit.
Control subclass extension. The template method only calls the operation at specific points, so that the extension can only be performed at these points. For example, the preceding runBare () method applies the setUp method only before runTest. If you do not want subclass to modify the framework of your template method definition, you can do this in two ways: first, your template method is not reflected in the API; 2. Set your template method to final.
We can see that the template method can be used to extract the public behavior of the Code for reuse. In the template method mode, the template method of the parent class controls the specific implementation of the subclass. In this way, you do not need to have much knowledge about business processes when implementing child classes.