PHP design pattern Decorator Mode _php Tutorial

Source: Internet
Author: User
Tags zend framework
Introduced
The decorator mode dynamically attaches responsibility to the object. To extend functionality, decorators provide a more resilient alternative than inheritance.

Mind Mapping

There is such a project to make a restaurant ordering system. The initial code structure is this way. There are many beverage inherited classes, and now the problem is that the price of milk has risen, so all the related classes, we have to adjust, such as the Milk,sugarandmilk class, there are many, we need to modify the method of the class one by another-developers do this kind of thing every time , it's going crazy! So we have to change the existing structure. The following diagram is a schematic, the actual figure, can not be so simple.

Design issues:

1 "Class number explosion, there are many classes, difficult to maintain;
2 "The whole design is rigid;
3 The new functionality added by the base class cannot be used in subclasses;
There are many ways to reuse class methods, such as inheritance, composition, and delegation. Why do you always get used to inheritance? I think the Zend Framework also has this habit! Each time you find the corresponding method, always go up. --Off-topic!!!!
Later, after a panel study, we decided to pull out the basic classes, for example, we made coffee into a separate class, other coffees, such as milk and coffee, and sweet coffee, and we only packed the material in a single class.
Improved design:

Detailed
1 "For drinks, we directly inherit the beverage class, directly put the quotation into the drinks category inside;
2 "and for some special drinks that need to be added, we do the cumulative operation. For example, I would like a cup of milk coffee, then the total Price = Coffee price + milk price
3 "It is easy to know the price of a different drink.
Code
Copy CodeThe code is as follows:
Abstract class beverage{
Public $_name;
Abstract public function cost ();
}
The decorated person class
Class Coffee extends beverage{
Public Function __construct () {
$this->_name = ' Coffee ';
}
Public function cost () {
return 1.00;
}
}
The following three classes are decorator related classes
Class Condimentdecorator extends beverage{
Public Function __construct () {
$this->_name = ' condiment ';
}
Public function cost () {
return 0.1;
}
}
Class Milk extends condimentdecorator{
Public $_beverage;
Public function __construct ($beverage) {
$this->_name = ' Milk ';
if ($beverage instanceof beverage) {
$this->_beverage = $beverage;
}else
Exit (' Failure ');
}
Public function cost () {
return $this->_beverage->cost () + 0.2;
}
}
Class Sugar extends condimentdecorator{
Public $_beverage;
Public function __construct ($beverage) {
$this->_name = ' Sugar ';
if ($beverage instanceof beverage) {
$this->_beverage = $beverage;
}else{
Exit (' Failure ');
}
}
Public function cost () {
return $this->_beverage->cost () + 0.2;
}
}
Test case
1. Grab a cup OF coffee
$coffee = new coffee ();
2. Add some milk
$coffee = new Milk ($coffee);
3. Add some sugar
$coffee = new Sugar ($coffee);
printf ("Coffee total:%0.2f yuan \ n", $coffee->cost ());

Summarize
1. Decorators (Milk) and decorators (Coffee) must be of the same type. The aim is that the decorator must replace the adorned person.
2. Add behavior: When decorators and components are combined, new behaviors are added.
Off Topic:
1. The use of inheritance to design subclass behavior is statically determined at compile time, and all subclasses inherit the same behavior. For example, Lao Tzu want to learn some kung fu, see you kid will Taijiquan, Lao Tzu only need to inherit you, Lao Tzu will be Taijiquan-hehe, then I will become your son, it seems that inheritance is to pay the price.
2. Combination, we can extend the behavior of the object, and dynamically expand it at run time. With the combination we can always add to the object the methods we didn't think of when we were designing the superclass, without changing the existing code. For example, I now have no internal force, suction Dafa, the monks, nuns, monks of internal forces (behavioral objects) are sucked over, that in the struggle (running), I can use different internal forces at any time, but also can not be indiscriminate absorption of internal forces, otherwise you will be possessed!
3. Classes should be open to extensions and closed for modifications. If every part of us is designed with a decorator pattern, it's a bit wasteful for the entire framework, and you're making it harder to code. So when do you use this model? We are generally used to change the place frequently. So how do we know which ones are often changed? This will require our experience and your knowledge of the industry in which you are located. We suggest that we usually show more examples.
4. The decoration mode injects elasticity into the design, but also adds a large number of small classes to the design, which occasionally makes it difficult for others to understand the design.
5. When using decorator mode, be especially careful with the inserted decorator. Because the adorner pattern relies on a particular type (beverage).
6. To use the decorator mode very well, we also use the Factory mode and generator mode, but today we only say decorator mode. If you want to know more, listen to tell.
Reference: "Head First design mode"

http://www.bkjia.com/PHPjc/325090.html www.bkjia.com true http://www.bkjia.com/PHPjc/325090.html techarticle Introduction Decorator Mode dynamically attaches responsibility to an object. To extend functionality, decorators provide a more resilient alternative than inheritance. Mind map has such a project, do one ...

  • 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.