Table of Contents
PHP Design Pattern - Template Pattern
Home Backend Development PHP Tutorial PHP Design Pattern - Template Pattern_PHP Tutorial

PHP Design Pattern - Template Pattern_PHP Tutorial

Jul 13, 2016 am 09:51 AM
model template Design Patterns

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between design patterns and architectural patterns in Java framework The difference between design patterns and architectural patterns in Java framework Jun 02, 2024 pm 12:59 PM

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

Comparison of C++ templates and generics? Comparison of C++ templates and generics? Jun 04, 2024 pm 04:24 PM

The difference between templates and generics in C++: Templates: defined at compile time, clearly typed, high efficiency, and small code size. Generics: runtime typing, abstract interface, provides flexibility, low efficiency.

What are the common applications of C++ templates in actual development? What are the common applications of C++ templates in actual development? Jun 05, 2024 pm 05:09 PM

C++ templates are widely used in actual development, including container class templates, algorithm templates, generic function templates and metaprogramming templates. For example, a generic sorting algorithm can sort arrays of different types of data.

PHP Design Patterns: Test Driven Development in Practice PHP Design Patterns: Test Driven Development in Practice Jun 03, 2024 pm 02:14 PM

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

Limitations of C++ templates and how to circumvent them? Limitations of C++ templates and how to circumvent them? Jun 02, 2024 pm 08:09 PM

Limitations of C++ templates and how to circumvent them: Code bloat: Templates generate multiple function instances, which can be circumvented through the optimizer, variable template parameters, and compile-time conditional compilation. Long compilation time: Templates are instantiated at compile time, which can avoid defining template functions in header files, instantiating them only when needed, and using PIMPL technology to avoid them. Type erasure: Templates erase type information at compile time, which can be circumvented through template specialization and run-time type information (RTTI).

Application of design patterns in Guice framework Application of design patterns in Guice framework Jun 02, 2024 pm 10:49 PM

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

How do C++ templates work? How do C++ templates work? Jun 03, 2024 pm 05:34 PM

Templates in C++ allow writing reusable code with the syntax, instantiated when called. Template specializations provide special implementations for specific types. In practice, templates can be used to sort arrays of different types, such as in the insertion sort algorithm.

What are the advantages and disadvantages of using design patterns in java framework? What are the advantages and disadvantages of using design patterns in java framework? Jun 01, 2024 pm 02:13 PM

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

See all articles