PHP Simple Decorator Pattern Implementation code

Source: Internet
Author: User
This paper mainly introduces the PHP simple decorator pattern implementation and usage, combined with concrete example form analysis of the PHP decorator mode principle, implementation and use of the method, the need for friends can refer to, hope to help everyone.


<?php//Decorator mode-on the structure of the original class, the function of the class is supplemented//weapon-based class abstract class weapon{abstract public function descriptions (); Abstract public function cost ();}  Sword class Glave extends weapon{public Function descriptions () {return ' Glave ';  } Public Function cost () {return ' 100 ';  }}//Dagger class Knife extends weapon{public Function descriptions () {return __class__;  } Public Function cost () {return ' 80 ';  }}//Axe class Axe extends weapon{public Function descriptions () {return __class__;  } Public Function cost () {return ' 200 ';  }}//Attribute Class Property extends weapon{protected $_weapon = null;  protected $_price = 0;  protected $_descriptions = ";  Public function __construct (weapon $weapon) {$this->_weapon = $weapon;  } Public Function cost () {return $this->_weapon->cost () + $this->_price;  The Public Function descriptions () {return $this->_weapon->descriptions (). $this->_descriptions; }}//Power Attribute Class Strength extends property{protected $_price = 30; Protected $_descriptions = ' + Strength ';}  Agile Attribute Class Agility extends property{protected $_price = 50; Protected $_descriptions = ' + Agility ';}  Intellectual properties Class Intellect extends property{protected $_price = 20; Protected $_descriptions = ' + Intellect ';} $weapon = new Agility (New Strength (new Glave ())), Echo $weapon->cost (), Echo $weapon->descriptions () ;

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.