Learning php design mode php implementation Template method mode

Source: Internet
Author: User
This article mainly introduces the Template method mode in the php design mode and implements the Template method mode using php. if you are interested, refer to it. I. intention
Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The Template Method allows the subclass to redefine certain specific steps of an algorithm without changing the structure of an algorithm [GOF95]
II. Structure of template method mode

III. main roles in template method mode
Abstract template (AbstractClass) role:Define one or more abstract methods for subclass implementation. These abstract methods are called basic operations. they are components of top-level logic.
Define a Template method. This template method is generally a specific method, which provides the skeleton of the top-level logic, and the logical composition step is in the corresponding abstract operation, these operations will be postponed to the subclass implementation. At the same time, the top-level logic can also call specific implementation methods.

Specific template (ConcrteClass) role:One or more abstract methods of the parent class are implemented as the composition of the top-level logic.

Each abstract template can correspond to multiple specific templates, and each specific template has its own implementation of abstract methods (that is, components of top-layer logic, so that the implementation of the top-level logic is different.
IV. applicable scenarios of template method mode
1. implement the unchanged part of an algorithm at one time, and leave the variable behavior to the subclass for implementation.
2. common behaviors in each subclass should be extracted and centralized into a common parent class to avoid code duplication.
3. control subclass extension.
V. Template method mode and other modes
1. strategy mode: The Template method uses inheritance to change the algorithm part, and the policy mode uses delegation to change the entire algorithm. The difference lies in the differences between closed changes. one part of a change is the whole.
2. factory method mode: The Factory Method mode is often called by the template method.
VI. PHP example of template method mode

<? Php/*** abstract template role * defines the abstract method as part of the top-level logic, and the subclass implements * defines the Template method as the shelf of the top-level logic, call the basic method to assemble the top-level logic */abstract class AbstractClass {/*** Template method call the basic method to assemble the top-level logic */public function templateMethod () {echo 'templatemethod begin.
'; $ This-> primitiveOperation1 (); $ this-> primitiveOperation2 (); echo 'templatemethod end.
';}/*** Basic method 1 */abstract protected function primitiveOperation1 ();/*** basic method 2 */abstract protected function primitiveOperation2 ();} /*** specific template role * implements the abstract method of the parent class */class ConcreteClass extends AbstractClass {/*** basic method 1 */protected function primitiveOperation1 () {echo 'primitiveoperation1
';}/*** Basic method 2 */protected function primitiveOperation2 () {echo 'primitiveoperation2
';}}/*** Client */class Client {/*** Main program. */public static function main () {$ class = new ConcreteClass (); $ class-> templateMethod () ;}} Client: main ();?>

VII. Template method mode
The template method is a basic technology for code reuse. The Template method leads to a reflection control structure, which refers to the operation of a parent class that calls subclass.
Implementation process: prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement different residual logics.
VIII. Principles of restructuring
The principle that should be followed during refactoring is to move the behavior to the high-end of the structure, and to move the state to the low-end of the structure as far as possible.
1. define a class for behaviors rather than states.
2. the implementation behavior is abstract state rather than specific state.
3. divide operations into layers.
4. postpone the confirmation of the status to the subclass. In the parent class, if you need state attributes, you can call the abstract value method and place the implementation of the abstract value method into a specific subclass.
If you can follow the preceding rules, you can separate the interface from the implementation in the hierarchical structure, and separate the abstraction from the specific implementation to ensure that the code can be reused to the maximum extent.

The above is the code that uses php to implement the Template method mode. There are also some concepts about the template method mode, which will be helpful for your learning.

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.