Home > Backend Development > PHP Tutorial > PHP Design Pattern - Template Pattern_PHP Tutorial

PHP Design Pattern - Template Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 09:51:00
Original
854 people have browsed it

PHP Design Pattern - Template Pattern

The template pattern prepares an abstract class, implements part of the logic in the form of concrete methods and concrete constructs, and then declares some abstract methods to force the subclass to implement the remaining logic. Different subclasses can implement these abstract methods in different ways and thus have different implementations of the remaining logic. Develop a top-level logic framework first, and leave the details of the logic to specific subclasses.

UML class diagram:

Character:

Abstract Template Role (MakePhone): Abstract template class defines a specific algorithm process and some abstract methods that must be implemented by subclasses.

Concrete subclass role (XiaoMi): implements the abstract method in MakePhone. The subclass can have its own unique implementation form, but the execution process is controlled by MakePhone.

Core code:

<!--?php
/**
 * Created by PhpStorm--->
 * User  extends  Jang
 * Date  extends  2015/6/10
 * Time  extends  11  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.手机生产完毕!
Copy after login

; } public abstract function MakeScreen(); public abstract function MakeBattery(); public abstract function MakeCamera(); } //Xiaomi mobile phone class XiaoMi extends MakePhone { public function __construct($name='Xiaomi') { parent::__construct( $name); } public function MakeBattery() { echo Xiaomi battery production is completed!
; } public function MakeCamera() { echo Xiaomi camera production is completed!
; } public function MakeScreen() { echo Xiaomi screen production is completed!
; } } //Meizu mobile phone class FlyMe extends MakePhone { function __construct($name='Meizu') { parent::__construct($name); } public function MakeBattery() { echo Meizu battery production is completed!
; } public function MakeCamera() { echo Meizu camera production is completed!
; } public function MakeScreen() { echo Meizu screen production is completed!
; } }
Call the client test code:

header(Content-Type:text/html;charset=utf-8);
//-------------------------模板模式---------------------
require_once ./Template/Template.php;
$miui=new XiaoMi();
$flyMe=new FlyMe();

$miui->MakeFlow();
$flyMe->MakeFlow();
Copy after login

Applicable scenarios and advantages:

1. Complete a process or a series of steps with a consistent level of detail, but the implementation of individual steps at a more detailed level may be different at the same time. We usually consider using the template pattern to deal with it.

2. When immutable and variable behaviors are mixed together in the subclass implementation of a method, the immutable behavior will appear repeatedly in the subclass. We use the template pattern to move these behaviors to a single place, thus helping subclasses get rid of the tangle of repeated immutable behavior.

3. The template pattern reflects its advantages by moving unchanged behaviors to super abstract classes and removing duplicate code in subclasses. The template pattern provides a good platform for code reuse.

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

PHP object-oriented design patterns

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1015543.htmlTechArticlePHP Design Pattern - Template Pattern The template pattern prepares an abstract class and converts part of the logic into concrete methods and concrete construction forms Implement, then declare some abstract methods to force subclasses to implement...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template