Decorator mode in PHP design mode

Source: Internet
Author: User
DecoratorPattern allows you to add new features to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a package of existing classes.

Concept

The Decorator Pattern allows you to add new features to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a package of existing classes.

This mode creates a decoration class to wrap the original class and provides additional functions while maintaining the integrity of the class method signature.

UML diagram

Role

Abstract Component role (Component): defines an object interface to standardize the objects that are prepared to accept additional responsibilities. This allows them to dynamically add responsibilities to these objects.

ConcreteComponent: the decorator that defines a class to be decorated to add features. You can add some responsibilities to the class object.

Decorator: maintains an instance pointing to the Component Object and defines an interface consistent with the Component interface of the abstract Component role.

ConcreteDecorator: adds responsibilities to the component.

Applicable scenarios

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

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

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.

Code

 _ Name = $ name;}/*** interface implementation method */public function display () {echo "dress up: {$ this-> _ name}
";}}/*** Class Clothes all decorators parent class-clothing Class */class Clothes implements IComponent {protected $ component; /*** receive decoration object ** @ param IComponent $ component */public function decorate (IComponent $ component) {$ this-> component = $ component ;} /*** output */public function display () {if (! Empty ($ this-> component) {$ this-> component-> display ();}}} /*** The following is the decoration Class * // *** class Sneaker sneakers */Class Sneaker extends Clothes {public function display () {echo "sports shoes"; parent :: display () ;}/ *** Class Tshirt T shirt */class Tshirt extends Clothes {public function display () {echo "T-shirt"; parent: display ();}} /*** Class Coat */class Coat extends Clothes {public function display () {echo "Coat"; parent: display ();}} /*** Class Trousers pants */class Trousers extends Clothes {public function display () {echo "pants"; parent: display ();}} /*** Client test code */class Client {public static function test () {$ zhangsan = new Person ('Zhang San '); $ lisi = new Person ('Lee si'); $ sneaker = new Sneaker (); $ coat = new Coat (); $ sneaker-> decorate ($ zhangsan ); $ coat-> decorate ($ sneaker); $ coat-> display (); echo" "; $ Trousers = new Trousers (); $ tshirt = new Tshirt (); $ trousers-> decorate ($ lisi); $ tshirt-> decorate ($ trousers ); $ tshirt-> display () ;}} Client: test ();

Running result:

Coat, sports shoes, dress up: James
T-shirt pants Dress Up By: Li Si

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.