When you want to add functions or attributes to an existing class, you may first want to use inheritance. Inheritance can add additional functions to the class at the compilation stage, but using inheritance will generate some questions about creating too many classes, maintenance, and update:
A classic example of coffee: A coffee program has an abstract coffee class, Moka coffee, black coffee ", the "latte" inherits from the abstract coffee class. It seems that there is no problem with this design. But think about Starbucks coffee, you can choose a series of coffee condiments such as sugar, milk, and Moka when you order coffee, and additional Condiments are charged separately, therefore, Moka coffee with double sugar may need to inherit a class separately, the "Moka coffee with ordinary sugar and Moka" also needs to be inherited from a class separately. What will happen? Once there are many kinds of condiments, and there are also many basic types of coffee, black coffee, and latte, because the various coffee they combine require a separate class to represent them, it will be quite difficult to maintain them.
In another example, when you use some tool interfaces, for example, a window class, the draw () method of this class can be used to draw a window with a basic style, to add a vertical or horizontal scroll bar or border to a window, you may need to inherit the window class and add some functions, if we need a window with a vertical scroll bar, a window with a horizontal scroll bar, a window with a border, a window with both a vertical scroll bar and a horizontal scroll bar the window of the scroll bar and border, we need to derive so many classes by inheritance, and if the border style needs to be changed, we may need to modify the original code or create more classes by inheritance.
In the above cases, if we use the "decoration mode", the effect will be different. XD, when you want to dynamically add specific behaviors or attributes to an object while the program is running, you can consider using the "decoration mode" instead of using inheritance (adding behavior or attributes to the class during compilation). The class diagram of "decoration mode" is as follows:
Picture from: http://insideria.com/2009/10/decorator-design-pattern.html
Observe the class diagram above and you will see an abstract component class at the top layer. The left side of the second layer is the entity component class (for specific purposes, you can use it to create objects, is the base object to be decorated), the right side is the abstract decoration class, the third layer is the entity decoration class concretedecoratora to add additional features for the operation method, concretedecoratorb adds a new attribute. Here we need to think about some issues:
Q:
1. Why is the decorator class designed as abstract?
2. Why do entity component classes and abstract decoration classes inherit the component abstract class?
3. How can I use decorator to dynamically expand the functions of concretecomponent class objects? (This is the purpose of the decoration Mode)
A: below.
The decoration mode is very abstract to understand and is not easy to express in language. I read a lot of articles about the decoration mode on the Internet. If it is not implemented, it is really laborious to understand the code and look at the implementation code, take window as an example:
In this example, five classes correspond to the top class diagrams respectively:
Window class-> component, // Abstract Window class
Normalwindow-> concretecomponent, // inherits the object class from the window class
Windowdecorator-> decorator, // abstract decoration class
Scrolldecorator-> concretedecoratora, // inherits the entity decoration class from windowdecorator, and adds the scroll bar function to the window.
Borderdecorator-> concretedecoratorb // inherits the entity decoration class from windowdecorator and adds a border to the window.
Window code:
Public abstract class window <br/>{< br/> // window title <br/> string description = "unknowwindow "; </P> <p> // The abstract method for drawing a window. each subclass is responsible for implementing this method. <br/> public abstract void draw (); </P> <p> // method for obtaining the window title <br/> Public String getdescription () <br/>{< br/> return description; <br/>}< br/>}
Code of the normalwindow class:
Public class normalwindow extends window <br/>{< br/> Public normalwindow () <br/>{< br/> This. description = "normalwindow"; <br/>}< br/> @ override <br/> Public void draw () <br/>{< br/> system. out. print ("draw a normal window"); <br/>}< br/>}
Code of the windowdecorator class:
Public abstract class windowdecorator extends window <br/>{< br/> // set getdescriptiong to an abstract method, all decorative subclass must implement this method again <br/> public abstract string getdescription (); <br/>}< br/>
Scrolldecorator class code:
Public class scrolldecorator extends windowdecorator <br/>{< br/> window componentwindow; <br/> // constructor. Input a window object to initialize the componentwindow instance variable, <br/> // reference of the object to be decorated. <Br/> Public scrolldecorator (window componentwindow) <br/>{< br/> This. componentwindow = componentwindow; <br/>}</P> <p> // added the scroll bar function for draw. <br/> Public void draw () <br/>{< br/> componentwindow. draw (); <br/> system. out. print ("add a scrollbar"); <br/>}</P> <p> @ override <br/> Public String getdescription () <br/>{< br/> return componentwindow. getdescription () + "with scrollbar"; <br/>}< br/>}
Borderdecorator class code:
Public class borderdecorator extends windowdecorator <br/>{< br/> window componentwindow; <br/> // constructor. Input a window object to initialize the componentwindow instance variable, <br/> // reference of the object to be decorated. <Br/> Public borderdecorator (window componentwindow) <br/>{< br/> This. componentwindow = componentwindow; <br/>}</P> <p> // added the border function for draw. <br/> Public void draw () <br/>{< br/> componentwindow. draw (); <br/> system. out. print ("Add border"); <br/>}</P> <p> @ override <br/> Public String getdescription () <br/>{< br/> return componentwindow. getdescription () + "has border"; <br/>}< br/>}
Test code:
Public class test {<br/> Public static void main (string [] ARGs) <br/>{< br/> window normalwindow = new normalwindow (); <br/> system. out. println (normalwindow. getdescription (); <br/> // use a scroll bar for decoration <br/> window decoration wwithscrollbar = new scrolldecorator (normalwindow); <br/> system. out. println (descriwwithscrollbar. getdescription (); <br/> // border decoration <br/> window decoration wwithscrollbarandborder = new borderdecorator (descriwwithscrollbar); <br/> system. out. println (descriwwithscrollbarandborder. getdescription (); </P> <p> descriwwithscrollbarandborder. draw (); <br/>}< br/>}
Output:
Normalwindow
Normalwindow with scrollbar
Normalwindow with scrollbar has border
Draw a normal window Add a scrollbar Add border
Through the above Code and class diagram, we should be able to better understand the decoration mode. Q & A's answer section:
A:
1. simply put, some non-Abstract METHODS inherited from the component class in the decorator class need to be reset to abstract (abstract), such as the getdescription () method, because each subclass needs to implement this method again. Of course, this is only one aspect of the Code.
2. because of the implementation principle of the decoration mode, the decorator class has a reference instance variable of the component type, which stores the reference of the object to be decorated, it is initialized by passing in the reference instance variable of the component type as the parameter through the decorator constructor. Imagine if the decorator does not inherit from component and uses the window class as an example, normalwindow needs to add a vertical scroll bar, then the normalwindow object needs to be used as a parameter to pass in the decorator class with the vertical scroll bar function, however, if you add the vertical scroll bar: New scrolldecorator (normalwindow)
After execution, the normalwindow object still needs to be added with a border. What should I do? Obviously, after the decorator inherits the component class, the new scrolldecorator (normalwindow) itself is also of the window type and can be passed into another decorator class as the decorated object, for example, new borderdecorator (New scrolldecorator (normalwindow) in the test code ));
3. As in the preceding example, the decorated object is passed into the constructor of the decorator object as a parameter to initialize the reference variable of the component type in the decorator object. The normalwindow class inherits the self-Abstract Window class. The window class has an abstract draw () method. The normalwindow can generate a window without a scroll bar or a border, while the scrolldecorator is used as the decoration class object, the scroll bar function is added to the normalwindow class object. The draw () method is rewritten for the scrolldecorator object as follows:
The draw method inherited by normalwindow from window:
Public void draw () <br/>{< br/> system. Out. Print ("draw a normal window"); <br/>}
The scrolldecorator class overrides the draw method inherited from the window:
Window component; // reference of the decorated object, which is the normalwindow object <br/> Public void draw () <br/> {<br/> // The System in the previous code segment is executed first. out. print ("draw a normal window"); <br/> component. draw (); <br/> // This is an additional feature <br/> system. out. print ("add a scrollbar on this window"); <br/>}
The decoration mode seems difficult to express clearly in so few languages .. The code is quite understandable.
Related Articles:
Design Patterns uncovered: the decorator pattern:
Http://java.dzone.com/articles/design-patterns-decorator
Example of window and coffee: http://en.wikipedia.org/wiki/Decorator_pattern#Java
More practical application of decorative patterns: http://www.javaworld.com/javaworld/jw-12-2001/jw-1214-designpatterns.html