PHP design mode-Detailed description of the decorator mode instance, detailed description of the design mode

Source: Internet
Author: User

PHP design mode-Detailed description of the decorator mode instance, detailed description of the design mode

This article describes the decorator mode of PHP design mode. We will share this with you for your reference. The details are as follows:

The decorator mode is also called the decorator mode. The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.

UML class diagram:

Role:

Component Object interface: dynamically add roles to these objects
Parent class of all decorators: You need to define an interface consistent with the Component interface and hold a Component object. This object is actually the decorated object.
Specific modifier class: implements the function to be added to the decoration object. It is used to decorate a specific component object or another specific decoration object.

Code:

<? Php/*** Created by PhpStorm. * User: Jiang * Date: 2015/5/3 * Time: 11: 11 * // ** component object Interface * interface IComponent */Interface IComponent {function Display ();} /** object to be decorated * Class Person */class Person implements IComponent {private $ name; function _ construct ($ name) {$ this-> name = $ name ;} function Display () {echo "Dress Up: {$ this-> name} <br/> ";}} /** all decorators parent Class * class Clothes */Class Clothes implements IComponent {Protected $ component; function Decorate (IComponent $ component) {$ this-> component = $ component;} function Display () {if (! Empty ($ this-> component) {$ this-> component-> Display () ;}}// -------------------------------- specific decorator ---------------- class PiXie extends Clothes {function Display () {echo "leather shoes"; parent: Display () ;}} class QiuXie extends Clothes {function Display () {echo "sneakers"; parent: Display ();}} class Tshirt extends Clothes {function Display () {echo "T-shirt"; parent: Display () ;}} class Waitao extends Clothes {function Display () {echo "coat "; parent: Display ();}}

Call the client test code:

Header ("Content-Type: text/html; charset = UTF-8"); // ------------------------ decorator mode test code ------------------ require_once ". /Decorator. php "; $ Yaoming = new Person (" Yao Ming "); $ aTai = new Person (" A tster "); $ pixie = new PiXie (); $ waitao = new Waitao (); $ pixie-> Decorate ($ Yaoming); $ waitao-> Decorate ($ pixie); $ waitao-> Display (); echo "

Applicable scenarios:

1. You need to dynamically add functions to an object. These functions can be dynamically revoked.

2. A large number of functions are generated by the arrangement and combination of some basic functions to make the inheritance relationship unrealistic.

3. When the subclass generation method cannot be used for expansion. One case is that there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. Another scenario is that the class definition is hidden, or the class definition cannot be used to generate a subclass.

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.