Php design pattern Template (Template pattern)

Source: Internet
Author: User
Defines the algorithm skeleton in an operation, and delays some steps to the subclass, so that the subclass can not change the structure of an algorithm, but define some specific steps of the algorithm inheritance relationship due to its own defects, the experts have taken down the "evil" hat. 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.

The code is as follows:


/**
* 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
";
}

Public function Method2 ()
{
Echo "abstract Method2
";
}

Public function Method3 ()
{
Echo "abstract Method3
";
}

Public function doSomeThing ()
{
$ This-> Method1 ();
$ This-> Method2 ();
$ This-> Method3 ();
}
}

Class TemplateObject extends TemplateBase
{
}

Class TemplateObject1 extends TemplateBase
{
Public function Method3 ()
{
Echo "TemplateObject1 Method3
";
}
}

Class TemplateObject2 extends TemplateBase
{
Public function Method2 ()
{
Echo "TemplateObject2 Method2
";
}
}

// 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.