Learn PHP Design Patterns PHP Implementation Template method pattern _php Skills

Source: Internet
Author: User
Tags define abstract learn php php example

I. Intention
Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template method enables subclasses to redefine certain steps of the algorithm "GOF95" without altering the structure of an algorithm.
Second, template method pattern structure diagram

Iii. main roles in template method patterns
Abstract Template (abstractclass) role: defines one or more abstract methods to implement a subclass. These abstract methods are called basic operations, and they are part of the top-level logic.
Defines a template method. This template method is generally a concrete method that gives the skeleton of the top-level logic, and the logical constituent steps in the corresponding abstract operations, these operations will be deferred to the subclass implementation. At the same time, the top-level logic can also invoke specific implementation methods

specific template (concrteclass) role: One or more abstract methods that implement the parent class exist as the composition of the top-level logic.

Each abstract template can have multiple specific templates that correspond to each other, and each concrete template has its own implementation of an abstract method (that is, part of the top-level logic), making the implementation of the top-level logic different.
Four, template method patterns apply to Scenarios
1, a one-time implementation of the invariant part of an algorithm, and leave the mutable behavior to subclasses to implement.
2, the public behavior in each subclass should be extracted and lumped into a common parent class to avoid code duplication.
3, Control child class extension.
Five, template method mode and other schemas
1, policy mode (strategy mode): The template method uses inheritance to change the part of the algorithm, and the policy mode uses the delegate to change the entire algorithm. The difference is that the closed change is different, a change in the part, a change is the whole.
2, factory approach mode (Factory method Mode): Factory method mode is often called by template methods.
VI, template method mode php example

<?php/** * Abstract Template Role * Define abstract methods as part of top logic, subclass implements * Define template method as top logic shelf, invoke basic method to assemble top logic/abstract class AbstractClass {
    /** * Template method invokes the basic method of assembling the top-level logic/public function Templatemethod () {echo ' Templatemethod begin.<br/> ';
    $this->primitiveoperation1 ();
    $this->primitiveoperation2 ();
  Echo ' Templatemethod end.<br/> ';
 
   /** * Basic Method 1 */abstract protected function primitiveOperation1 ();
/** * Basic Method 2 */abstract protected function primitiveOperation2 (); /** * Specific Template Role * Abstract method to implement parent class * * Class Concreteclass extends abstractclass{/** * Basic Method 1/Protected function
  PrimitiveOperation1 () {echo ' primitiveoperation1<br/> ';
  /** * Basic Method 2 */protected function PrimitiveOperation2 () {echo ' primitiveoperation2<br/> ';
   }/** * Client/class Client {/** * Main program.
    */public static function main () {$class = new Concreteclass ();
$class->templatemethod ();  } client::main ();?> 

Vii. Template Method Patterns
Template method is a basic technique for code reuse, which leads to a reflective control structure, which refers to the operation of a parent class calling a subclass.
Its implementation: prepares an abstract class, implements part of the logic in the form of concrete methods and constructs, and then declares some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic.
Viii. Principles of reconstruction
The principle you should follow when refactoring is to move the behavior to the top of the structure and move the state to the lower end of the structure as much as possible.
1. A class should be defined to act rather than state.
2, in the implementation of the behavior is to use the abstract state rather than the specific state.
3, to the operation of the division level.
4. Postpone the confirmation of the status to the subclass. In the parent class, if a state attribute is required, the abstract value method can be invoked, and the implementation of the abstract value method is placed in a specific subclass.
If you can follow the above, then you can separate the interface from the implementation in the hierarchy, separating the abstraction from the concrete, thus ensuring that the code can be reused to the maximum extent possible.

The above is the use of PHP to implement template method pattern code, there are some of the template method model of the concept of differentiation, I hope to help you learn.

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.