3. Decorator Mode
The decorator mode dynamically attaches responsibility to the object. To extend functionality, decorators provide a more resilient alternative than inheritance.
I. Problem INTRODUCTION
Coffee Shop Class Design:
A beverage base class, a variety of beverage classes inherit this base class and calculate their price.
To add a variety of spices in the beverage, consider adding some Boolean variables to the base class to indicate whether to add a variety of spices, the cost of the base class () to calculate the price of the various spices, the sub-category cover costs (), and in which the cost of the Super-class (), plus the price of a particular beverage, to calculate the price
Disadvantages: The number of classes explosion, the new function of the base class addition does not apply to all subclasses, seasoning price changes, the emergence of new spices will require changes to existing code, and some sub-categories are not suitable for certain spices and other situations ...
Two. Key points
1. Classes should be open to extensions and closed for modifications.
Our goal is to allow classes to be easily extended and to match new behavior without modifying existing code. The advantage is that the design is resilient to change and can accept new features to respond to changing needs.
2. The decorator and the decorated object have the same super-type.
2. Combinations and delegates can be used to dynamically add new behaviors when exercising.
3. The decorator can add his or her own behavior in front/behind the act of the decorator, or even replace it with the entire behavior of the decorator and achieve a specific purpose.
4. A component can be packaged with countless decorators.
5. Decorators are generally transparent to the customer of the component, unless the client program relies on the specific type of the component.
6. Decorators can cause many small objects to appear in the design, which can complicate the program if overused.
Three. Use decorator mode to solve problems
Ways to solve a coffee shop drink problem:
Take the beverage as the main body, then "decorate" the beverage with seasoning at run time.
For example, customers want Mocha (Mocha) and milk foam (Whip) deep-roasted coffee (darkroast):
Darkroast inherits from Beverage and has a cost () method.
The first step is to start with the Darkroast object;
In the second step, the customer wants Mocha, so create a Mocha decorator object and use it to wrap the Darkroast object (wrap) up;
In the third step, the customer wants the milk bubble, so build a whip object and use it to wrap the Mocha object, (Mocha and whip also inherit from beverage, there is a cost () method);
Finally, for the customer to calculate money, by calling the most outer ring decorator (Whip) cost () can be. The cost of Whip () will first be entrusted to the object it decorates (Mocha) to calculate the price, and then add the price of the milk bubble. Mocha's cost () is similar.
Four. UML diagrams
The source class is a decorated class, and the decorator class is a decorative class that can dynamically add some functionality to the source class.
Five. Implementing the Code
Sourceable Interface:
Public interface sourceable {public void method ();
Be decorated class:
Public class Implements sourceable { @Override publicvoid method () { System.out.println (" The original method! " ); } }
Decoration Category:
Public classDecoratorImplementssourceable {Privatesourceable Source; PublicDecorator (sourceable source) {Super(); This. Source =source; } @Override Public voidmethod () {System.out.println ("Before decorator!"); Source.method (); System.out.println ("After decorator!"); } }
Test class:
Public class decoratortest { publicstaticvoid main (string[] args) { New Source (); New Decorator (source); Obj.method (); } }
Output:
Before decorator!
The original method!
After decorator!
Six. Application Scenarios
1. You need to extend the functionality of a class.
2. Dynamically adds functionality to an object and can also be deactivated dynamically. (Inheritance cannot do this, the inherited functionality is static and cannot be dynamically deleted.) )
Cons: Produce too many similar objects, not easy to debug!
Seven. Expansion
Implementation class diagram in Head first design mode:
The decorator pattern implementation class diagram in the Java.io package:
Reference:
"Head First design mode"
http://blog.csdn.net/zhangerqing/article/details/8239539
Http://www.cnblogs.com/mengdd/archive/2013/01/03/2843439.html
One design pattern per day (3): Decorator mode