Learning php design patterns php decorator)

Source: Internet
Author: User
Tags php example
This article mainly introduces the decoration mode in the php design mode and implements the decoration mode using php. if you are interested, you can refer to the dynamic introduction of some additional responsibilities for an object. The Decorator mode is more flexible than the subclass generation function (GOF95]
The decoration mode dynamically attaches more responsibilities to an object in a transparent manner to the customer. In other words, the client does not think that the objects are different before and after decoration. The decoration mode can extend the functions of objects without creating more child classes.
I. decorative pattern structure

II. main roles in decoration mode
Abstract Component role:Defines an object interface to standardize the objects that are prepared to receive additional responsibilities, so that these objects can be dynamically added with responsibilities.
Specific Component (Concrete Component) role:Define a class to receive additional responsibilities.
Decorator role:Hold a pointer to the Component Object and define an interface consistent with the Component interface.
Specific decoration (Concrete Decorator) role:Adds additional responsibilities to component objects.
III. Advantages and disadvantages of the decoration mode
Decorative modeAdvantages:
1. more flexible than static inheritance;
2. avoid having too many features in the class at the upper level of the hierarchy.
Decorative modeDisadvantages:
1. the decoration mode produces more objects than the inheritance relationship. And these objects seem like imagination, making it difficult to find errors.
IV. applicable scenarios of the decoration mode
1. add roles to a single object dynamically and transparently without affecting other objects.
2. handle unrecoverable responsibilities. that is, you need to dynamically add functions to an object and these functions can be undo dynamically.
3. when it is not possible to generate subclass methods 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.
V. decoration mode PHP example

<? Php/*** abstract Component role */interface Component {/*** example method */public function operation ();} /*** decoration role */abstract class Decorator implements Component {protected $ _ component; public function _ construct (Component $ component) {$ this-> _ component = $ component;} public function operation () {$ this-> _ component-> operation ();}} /*** specific decoration class A */class ConcreteDecoratorA extends Decorator {public function _ construct (Component $ component) {parent :__ construct ($ component );} public function operation () {parent: operation (); // call the decoration class operation $ this-> addedOperationA (); // Newly added Operation}/*** newly added Operation A, that is, the decoration function */public function addedOperationA () {echo 'add Operation
';}}/*** Specific decoration class B */class ConcreteDecoratorB extends Decorator {public function _ construct (Component $ component) {parent ::__ construct ($ component );} public function operation () {parent: operation (); $ this-> addedOperationB () ;}/ *** newly added operation B, that is, the decorative function */public function addedOperationB () {echo 'add Operation B
';}}/*** Specific Component */class ConcreteComponent implements Component {public function operation () {echo' Concrete Component operation
';}}/*** Client */class Client {/*** Main program. */public static function main () {$ component = new ConcreteComponent (); $ decoratorA = new ConcreteDecoratorA ($ component); $ decoratorB = new ConcreteDecoratorB ($ decoratorA ); $ decoratorA-> operation (); $ decoratorB-> operation () ;}} Client: main () ;?>

The above is the code that uses php to implement the decoration mode. There are also some concepts about the decoration mode, and I hope it will be helpful for everyone's learning.

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.