PHP design mode template (templates mode) _php Tutorial

Source: Internet
Author: User
Due to their own shortcomings, the inheritance relationship was deducted by experts as "evil" hats. "Use delegated relationships instead of inheritance", "Try to use interface implementations instead of abstract class inheritance" and so on expert warnings, let us these rookie to inherit "the". In fact, there are many advantages of inheritance. It seems that the disadvantage of being abused by everyone is more obvious. The rational use of inheritance relationship, or can be a good role in the design of your system. and the template method pattern is one of the use examples.

GOF defines the skeleton of an algorithm in an operation to the template method pattern, and delays some steps into subclasses. Enables subclasses to 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 that you design based on your needs. Specific steps are those that may have variables in the content.

As you can see, the template method pattern is also designed to subtly address the impact of changes on the system. The use of template methods to enhance system extensibility, minimizing the impact of changes on the system. This, in the following example can be clearly seen.
Copy CodeThe code is as follows:
/**
* Template mode
*
* Define an algorithm skeleton in an operation, and defer some steps into subclasses so that subclasses can define some specific 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
";
}
}

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

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

AbstractClass (abstract Class): One or more abstract methods are defined for specific subclasses to implement them, and a template method is implemented to define the skeleton of an algorithm. The template method not only invokes the previous abstract method, but also invokes other operations, as long as it can accomplish its own mission.

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

Based on the analysis of the above definition and the description of the example, you can see that the template method is suitable for the following situations:

One-time implementation of an invariant part of the algorithm, and the variable behavior is left to subclass to achieve.
The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication. In fact, this can be said to be a good coding habit.
Controls the subclass extension. The template method invokes the operation only at a specific point, so that only those points are allowed to be extended. For example, the Runbare () method above applies the Setup method only in front of Runtest. If you do not want the subclass to modify the framework of your template method definition, you can do it in two ways: one is not to show your template method in the API, and the other is to set your template method to final.
It can be seen that using the template method pattern can extract the common behavior of code and achieve the purpose of reuse. Also, in the template method pattern, the template method of the parent class is used to control the specific implementation in the subclass. This way you do not need to have much knowledge of the business process when implementing subclasses.

http://www.bkjia.com/PHPjc/323607.html www.bkjia.com true http://www.bkjia.com/PHPjc/323607.html techarticle due to their own shortcomings, the inheritance relationship was deducted by experts as "evil" hats. "Use delegated relationships instead of inheritance", "Try to use interface implementations instead of abstract class inheritance," and so on ...

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