this article illustrates the decorator pattern of the PHP design pattern. Share for you for your reference, specific as follows:

decorator mode also called decorator mode. Decoration mode is the function of dynamically extending an object without having to change the original class file and use inheritance. It is by creating a package object, that is, the decoration to wrap the real object.

http://files.jb51.net/file_images/article/201802/201827122524300.jpg? 201817122544" alt= ">

component interface: can give these objects dynamically add
all decorator class : need to define a component interface and consistent interface, and holds a Component object, the object is in fact the decorative objects.
specific decorator class : implement the functionality that is specifically added to the decorated object. It is used to decorate a specific component object or another specific decorator object.

 < specific code: PHP Created by PhpStorm.? / * * * * User: Jiang Date: * 2015/5/3 * Time: * / 11:11 / * * * Interface * / IComponent interface component object interface IComponent function (Display);} {/ * * * Class Person * / to be decorative object class Person implements IComponent {private $name function; __construct ($name) {$this-> name=$name; function (Display) {echo} "dress: {$this-> name}< br/>}}; / * * all * Class Clothes decorator class Clothes implements / class IComponent function Decorate {protected $component; (IComponent $component) {$this->} function; component=$component; Display (if (EMP) {! Ty ($this-> component)) {$this-> component-> (Display);}}} //------------------------------ decorator PiXie extends ---- class specific Clothes (Display) {function {echo "shoes"; parent:: Display (class QiuXie extends);}} Clothes {function {echo (Display) parent:: "shoes"; Display (class Tshirt extends);}} Clothes {function {echo (Display) "T-shirt"; parent:: Display (class Waitao extends);}} Clothes {function {echo (Display) "coat"; parent:: Display (

);}} is called the client test code:

 header ("Content-Type:text/html; charset=utf-8"); //------------------------ decorator pattern test code c require_ Once "./Decorator/Decorator.php"; $Yaoming=new Person ("Yao Ming"); $aTai=new Person ("A test"); $pixie=new (PiXie); $waitao=new (Waitao); $pixie-> Decorate; $waitao-> Decorate ($Yaoming) ($pixie); Display ($waitao->); echo; < hr/> $qiuxie=new; QiuXie ("$tshirt=new (Tshirt));; $qiuxie-> Decorate; $tshirt-> Decorate ($aTai) ($qiuxie); $tshirt-> Display; 

) style=" font-size: medium > "for the scene:

1. to an object dynamically add function, these functions can be dynamic revocation.

2. needs to increase the very large number of functions generated by the combination of some basic functions, so that the inheritance relationship becomes unrealistic.

3. is extended when the generation subclass cannot be used. One case is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, making the number of subclasses increasing explosively. Another situation may be that class definitions are hidden, or class definitions can't be used to generate subclasses.

more PHP on the relevant content of interested readers can see the special station: "php object-oriented programming tutorial" , PHP " basic grammar tutorial "PHP array (Array) operating skills Daquan ," "php string (string) " php+mysql database operation tutorial " and "php " common database operation skills summary

< p> hope the PHP program designed to help to everyone.

This paper fixed link:http://www.script-home.com/detailed-explanation-of-the-decorator-pattern-of-the-php-design-pattern.html | Script Home | +Copy Link

Article reprint please specify:Detailed explanation of the decorator pattern of the PHP design pattern | Script Home

You may also be interested in these articles!