Detailed explanation of using PHP decorator pattern
This time I will bring you PHPDecorator modeDetailed explanation of use, what are the precautions for using PHP decorator mode, the following is a practical case, let's take a look.
What is the decorator pattern
As a structural pattern, the decorator pattern is to add " Decoration".
Adapter pattern is to add an adapter class to the current structure. It converts the interface of one class into another one that the customer expects. Interfaces and adapters allow classes with incompatible interfaces to work well together.
Decorator pattern is to wrap an object to enhance new behaviors and responsibilities. Decorators are also called Wrapper (similar to adapter)
Some design patterns include an abstract class, and the abstract class also inherits another abstract class. There are only a few such design patterns, and decorator is one of them.
When to use the decorator pattern
Basically speaking, if you want to add new functionality to an existing object without affecting other objects, just You can use the Decorator Pattern. If you've gone to great lengths to create a website format for a client, and the main components are working perfectly, you don't want to have to reinvent the wheel and recreate the website when the client requests a new feature. For example, let's say you've After building the components originally requested by the customer, the customer has new needs and wants to include video functionality in the website. You don't need to rewrite the original components, you only need to "decorate" the existing components to add video functionality to them. That's it You can maintain the original functions, and you can also add new functions.
Some projects may sometimes need to be decorated, and sometimes do not want to be decorated. These projects reflect another important feature of the decorator design pattern. Assume that your basic website Development mode meets the requirements of most customers. However, some customers also want some specific features to meet their needs. Not everyone wants or needs these additional features. As a developer, you want what you create The website can meet the customer's business goals. Therefore, it needs to provide "customerization" features, that is, features provided for specific businesses. Using the decorator pattern, not only can it provide core functions, but it can also be "decorated" with unique functions required by customers. These core functions.
Simple Decorator Example
A web development company plans to build a basic website with some enhanced features. However, web developers know , although this basic plan works for most customers, it is likely that customers will want to improve further in the future. Using the decorator mode, you can easily add multiple specific decorators. In addition, because you can choose which decorators to add, enterprises Not only can the function be controlled, but also the cost of the project can be controlled.
Component interface
The Component participant is an interface, here, it is an abstract class IComponent. This The abstract class has only one attribute $site, and two abstract methods getSite()
and getPrice().Component
Participants specifically establish interfaces for specific components and the Decorator participant abstract class:
IComponent.php
<?php abstract class IComponent { protected $site; abstract public function getSite(); abstract public function getPrice(); }
Decorator interface
The decorator interface in this example may surprise you. This It is an abstract class, and it also extends another abstract class! The function of this class is to maintain a reference to the component interface (IComponent), which is done by extending IComponent:
Decorator.php
<?php abstract class Decorator extends IComponent { /* 任务是维护Component的引用 继承getSite()和getPrice() 因为仍然是抽象类,所以不需要实现父类任何一个抽象方法 */ }
The main function of the Decorator class is to maintain a reference to the component interface.
In all implementations of the decorator pattern, you will find that the specific components and decorators are the same interface. Their implementations may be different, and in addition to the properties and methods of the basic interface, components and decorators may have additional properties and methods.
Specific components
There is only one specific component in this example, which generates a website name and a basic website quote:
BasicSite.php
<?php class BasicSite extends IComponent { public function construct() { $this->site = "Basic Site"; } public function getSite() { return $this->site; } public function getPrice() { return 1200; } }
Both abstract methods are implemented using direct assignment, but the flexibility is not reflected in how to change the set value. In fact, the "Basic Site" value needs to be changed by adding a decorator value.
Specific decorator
这个例子中的具体装饰器与具体组件有相同的接口.实际上, 它们是从Decorator抽象类(而不是IComponent类)继承了这个接口. 不过,要记住, Decorator所做的就是继承IComponent接口.
Maintenance.php
<?php class Maintenance extends Decorator { public function construct(IComponent $siteNow) { $this->site = $siteNow; } public function getSite() { $format = "<br /> Maintenance"; return $this->site->getSite() . $format; } public function getPrice() { return 950 + $this->site->getPrice(); } }
这个装饰器Maintenance在改变了site的值, 还有包装的具体组件价格上还会增加它自己 的价格. 另个两个具体装饰器与Maintenance装饰器也类似
Video.php
<?php class Video extends Decorator { public function construct(IComponent $siteNow) { $this->site = $siteNow; } public function getSite() { $format = "<br /> Video"; return $this->site->getSite() . $format; } public function getPrice() { return 350 + $this->site->getPrice(); } }
DataBase.php
<?php class DataBase extends Decorator { public function construct(IComponent $siteNow) { $this->site = $siteNow; } public function getSite() { $format = "<br /> DataBase"; return $this->site->getSite() . $format; } public function getPrice() { return 800 + $this->site->getPrice(); } }
测试这个应用时,可以看到,在基本的价格之上还会增加各个装饰器的价格.另外还能指定装饰器名的格式, 增加了两个空格,使之缩进
装饰器实现中最重要的元素之五就是构造函数, 要为构造函数提供一个组件类型. 由于这里只有一个具体组件, 所有装饰器的实例化都会使用这个组件. 使用多个组件时, 装饰器可以包装应用中的一部分或全部组件, 也可以不包装任何组件.
客户
Client类并不是这个设计模式的一部分, 但是正确使用Client类至关重要.每个装饰器在实例化时"包装"组件, 不过, 首先必须创建一个要包装的对象, 这里是BasicSite类实例
Client.php
<?php function autoload($class_name) { include $class_name . '.php'; } class Client { private $basicSite; public function construct() { $this->basicSite = new BasicSite(); $this->basicSite = $this->WrapComponent($this->basicSite); $siteShow = $this->basicSite->getSite(); $format = "<br /> <strong>Total= $"; $price = $this->basicSite->getPrice(); echo $siteShow . $format . $price . "</strong>"; } private function WrapComponent(IComponent $component) { $component = new Maintenance($component); $component = new Video($component); $component = new DataBase($component); return $component; } } $worker = new Client();
wrapComponent()
方法检查传入的BasicSite实例, 以确保参数有正确的数据类型(IComponent), 然后分别实例化3个装饰器, 对该实例对象进行装饰.
Basic Site
Maintenance
Video
DataBase
Total= $3300
适配器和装饰器模式都有另外一个名字"包装器"(wrapper)".
适配器可以"包装"一个对象, 创建一个与Adaptee兼容的接口, 而无须对它做任何修改.
装饰器也可以"包装"一个组件对象, 这样就能为这个已胡的组件增加职责, 而无须对它做任何修改.
下面的代码展示了Client如何将组件对象($component)包装在装饰器(Maintence)中:
$component = new Maintenance($component);
类似于"接口", 在计算机编程中用到"包装器"时, 不同的上下文会有不同的用法和含义. 一般来讲, 在设计模式中使用"包装器"是为了处理接口的不兼容, 或者希望为组件增加功能,包装器就表示用来减少不兼容性的策略.
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of using PHP decorator pattern. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
