php design pattern Template of template pattern

  • 2020-05-09 18:15:59
  • OfStack

Due to its own defects, the inheritance relationship has been labeled as "evil" by experts. "Use delegation instead of inheritance," "try to use interface implementations instead of abstract class inheritance," and so on. In fact, inheritance has many advantages of its own. It's just that the abuse seems to be more obvious. The proper use of the inheritance relationship, or you can play a good role in the system design. The template method pattern is one example of this.

The GOF template method (Template Method) pattern defines the skeleton of the algorithm in an operation, deferring some steps to subclasses. Enables subclasses to redefine certain steps of an algorithm without changing its structure. The structure of the algorithm here can be understood as a business process that you design according to the requirements. Specific steps are those that may vary in content.

As you can see, the template method pattern is also designed to subtly address the impact of change on the system. The template method is used to enhance the system scalability and minimize the impact of changes on the system. This point is obvious in the following example.
 
<?php 
/** 
*  Template pattern  
* 
*  define 1 Algorithm skeleton in operation , And will be 1 These steps are deferred to subclasses , So that the subclass does not change 1 The structure of the algorithm can define some specific steps of the 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/>"; 
} 
} 

//  instantiation  
$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 concrete subclasses to implement; Moreover, we also need to implement a template method to define the skeleton of an algorithm. This template method can call not only the previous abstract method, but other operations as well, as long as it does its job.

ConcreteClass (concrete class) : implement the abstract methods in the parent class to complete the steps in the algorithm associated with a particular subclass.

Based on the analysis of the definitions above and the illustration of the examples, it can be seen that the template method is suitable for the following situations:

Implement the invariant part of an algorithm and leave the variable behavior to subclasses.
The common behavior in each subclass should be extracted and concentrated into a common parent class to avoid code duplication. This is actually a good coding habit.
Control subclass extension. Template methods invoke operations only at specific points, allowing them to be extended only at those points. For example, the runBare () method above only applies the setUp method before the runTest method. If you don't want to subclass your template method definition framework, you can do it in two ways: 1) API does not include your template method; 2. Set your template method to final.
It can be seen that the template method pattern can be used to extract the common behavior of the code and achieve the purpose of reuse. Furthermore, in the template method pattern, it is the parent class's template methods that control the implementation in the subclass. You don't need to know much about the business process at all to subclass.

Related articles: