PHP design mode-template mode

Source: Internet
Author: User

Statement: This series of blog reference "Big Talk design mode", author Geoscience.


The template schema prepares an abstract class, implements some of the logic in concrete ways and concrete 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. First, a top-level logical framework is developed, and the details of the logic are left to the specific subclasses to implement.


UML Class Diagrams:


Role:

Abstract Template Role ( Makephone ): abstract template class, which defines a specific algorithm flow and some abstract methods that must be implemented for subclasses.

specific sub-class roles ( Xiaomi ): Implement Makephone the subclass can have its own unique implementation form, but the execution process is subject to the Makephone Control .

Core code:

<?php/** * Created by phpstorm-> * User extends Jang * Date extends 2015/6/10 * Time extends one extends 06 */    Abstract class makephone{protected $name;    Public function __construct ($name) {$this->name= $name;        Public Function Makeflow () {$this->makebattery ();        $this->makecamera ();        $this->makescreen (); echo $this->name. " Mobile phone production finished!    
To invoke the client test code:

Header ("Content-type:text/html;charset=utf-8");//-------------------------template mode---------------------require_ Once "./template/template.php"; $miui =new Xiaomi (); $flyMe =new flyMe (); $miui->makeflow (); $flyMe->makeflow ();


application Scenarios and advantages:

1 , a process or series of steps that complete a level of detail, but the implementation of individual steps at a more detailed level may not be the same. We usually consider using template mode for processing.

2 , when immutable and mutable behaviors are mixed together in a subclass implementation of a method, the invariant behavior is repeated in the subclass, and we move the behavior to a single place through the template pattern, which helps the subclass to get rid of the entanglement of repeated invariant behavior.

3 , template mode is the advantage of removing repetitive code from subclasses by moving unchanged behavior to super abstract classes. Template mode provides a good code reuse platform.

Welcome to follow my video course, the address is as follows, thank you.


PHP Object-oriented design pattern

PHP design mode-template mode

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.