Learn PHP Design patterns PHP Implementation adorner mode (decorator) _php tips

Source: Internet
Author: User
Tags class definition learn php php example

Dynamically adds some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality "GOF95"
Decoration mode is a dynamic way of attaching more responsibilities to an object in a transparent manner to the customer. This means that the client does not feel that the object is different before and after the decoration. The decorative pattern can extend the functionality of an object without using the creation of more subclasses.
First, decorate the pattern structure diagram

Second, main role in decorate mode
abstract widget (Component) Role: defines an object interface to standardize the objects that are ready to receive additional responsibilities, This allows you to dynamically add responsibilities to these objects.
Concrete Widget (concrete Component) Role: defines a class that will receive additional responsibilities.
Decoration (decorator) role: holds a pointer to a component object and defines an interface that is consistent with the component interface.
the specific decoration (concrete decorator) role: responsible for adding additional responsibilities to the Widget object.
Three, advantages and disadvantages of decorative mode
decorative mode Benefits:
1, more flexible than static inheritance;
2, Avoid having too many features for classes at the top of the hierarchy
Decorative mode disadvantage:
1, using decorative mode produces more objects than using inheritance relationships. And these objects seem to be very imaginative, making it difficult to find errors.
Four, decorated mode applies to scenarios
1, adding responsibilities to a single object in a dynamic and transparent manner without affecting other objects.
2, to handle the functions that can be undone, that is, to dynamically add functionality to an object and that these functions can be undone dynamically.
3, when the method for generating subclasses cannot be expanded. One scenario 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 explode. Another scenario might be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
Five, decorate mode PHP example

<?php/** * Abstract Widget Role * * interface Component {/** * Example method */Public function operation ();}
 
 /** * Decorative 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);
  The public Function operation () {parent::operation ();//Invoke the operation of the adornment class $this->addedoperationa ();//new action added}/**
 * Newly added Operation A, that is, the function of the decoration/public Function Addedoperationa () {echo ' Add Operation a <br/> ';
  }/** * 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 <br/> '; }/** * Specific Widget/class Concretecomponent implements component{Public function operation () {echo ' concrete Comp
 Onent operation <br/> ';
  }/** * 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 use of PHP to implement the decorative mode of the code, there are some of the concept of decorative patterns, I hope to help you learn.

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.