PHP design mode Template (template mode) _php tips

Source: Internet
Author: User
Due to its own shortcomings, the inheritance relationship has been buttoned up by experts on the "evil" hat. "Use delegated relationships instead of inheritance relationships", "Try to use interface implementations instead of abstract class inheritance" and so on experts warn, let us these rookie to inherit "Respect". In fact, inheritance or have a lot of their own advantages. It seems that the shortcomings are more obvious than those abused by everyone. Reasonable use of the inheritance relationship, or to your system design can play a good role. The template method pattern is one of the examples of usage.

Gof the template Method (Template) pattern to define the skeleton of an algorithm in an operation and delay some steps into subclasses. So that subclasses can redefine some specific steps of the algorithm without altering the structure of an algorithm. The structure of the algorithm here can be understood as a business process designed for you based on requirements. Specific steps are those that may have variables in the content.

As can be seen, template method patterns are designed to skillfully address the impact of changes on the system. Use the template method to increase system extensibility and minimize the impact of changes on the system. This, in the following example can be clearly seen.
Copy Code code as follows:

<?php
/**
* Template mode
*
* Define an algorithm skeleton in an operation, and delay some steps into subclasses so that subclasses can define certain steps of the algorithm without altering 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/>";
}
}

Instantiation of
$objTemplate = new Templateobject ();
$objTemplate 1 = new TemplateObject1 ();
$objTemplate 2 = new TemplateObject2 ();

$objTemplate->dosomething ();
$objTemplate 1->dosomething ();
$objTemplate 2->dosomething ();

AbstractClass (abstract Class): Defines one or more abstract methods for specific subclasses to implement them, and implements a template method to define the skeleton of an algorithm. This template method not only calls the previous abstract method, but also can invoke other operations, as long as it can fulfill its own mission.

Concreteclass (Concrete Class): implements the abstract methods in the parent class to complete the steps associated with a particular subclass in the algorithm.

Based on the analysis of the definition and the description of the example, it can be seen that the template method is suitable for the following situations:

Implement an invariant part of an algorithm at once and leave the variable behavior to subclasses.
Public behavior in each subclass should be extracted and lumped into a common parent class to avoid code duplication. In fact, this can be said to be a good coding habit.
Controls child class extensions. Template methods invoke operations only at specific points, allowing extensions only at those points. For example, the above Runbare () method only applies to the Setup method before runtest. If you don't want subclasses to modify the framework of your template method definition, there are two ways you can do it: first, it doesn't reflect your template approach in the API, and you can put your template approach to final.
It can be seen that using template method pattern can extract the public behavior of code and achieve the purpose of reuse. Furthermore, in the template method pattern, the parent class's template method controls the specific implementation in subclasses. So when you implement subclasses, you don't need to know much about the business process at all.

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.