PHP design pattern: decorator pattern code example, php design pattern

Source: Internet
Author: User

PHP design pattern: decorator pattern code example, php design pattern

Definition:

The modifier mode dynamically extends the class functions without modifying the original class code and inheritance. The traditional programming mode is that subclass inherits the parent class implementation method overload. With the decorator mode, you only need to add a new decorator object, which is more flexible and avoids too many classes and layers.

Role:

Component (base class of the decorated object)
ConcreteComponent (specific object to be decorated)
Decorator)
ContreteDecorator (Specific modifier class)

Sample Code:

// Decorator base class interface Component {public function operation ();} // Decorator base class abstract class Decorator implements Component {protected $ component; public function _ construct (Component $ component) {$ this-> component = $ component;} public function operation () {$ this-> component-> operation ();}} // The modifier class ConcreteComponent implements Component {public function operation () {echo 'do operation '. PHP_EOL ;}}// specific decoration class Aclass ConcreteDecoratorA extends Decorator {public function _ construct (Component $ component) {parent ::__ construct ($ component);} public function operation () {parent: operation (); $ this-> addedOperationA (); // Newly Added Operation} public function addedOperationA () {echo 'add operation '. PHP_EOL ;}}// the decoration class Bclass ConcreteDecoratorB extends Decorator {public function _ construct (Component $ component) {parent ::__ construct ($ component);} public function operation () {parent: operation (); $ this-> addedOperationB ();} public function addedOperationB () {echo 'add Operation B '. PHP_EOL ;}} class Client {public static function main () {/* do operation Add Operation A */$ decoratorA = new ConcreteDecoratorA (new ConcreteComponent ()); $ decoratorA-> operation ();/* do operation Add Operation A Add Operation B */$ decoratorB = new ConcreteDecoratorB ($ decoratorA); $ decoratorB-> operation ();}} client: main ();

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.