It took a long time to understand this model, but it was a little hard to understand at first.
Definition: Adding additional responsibilities to an object dynamically, like painting a wall. Using Decorator mode is more flexible than extending functions by generating subclasses.
Design intention: Inheritance is usually used to expand functions. If there are many kinds of functions that need to be expanded, many subclasses will be generated, which will increase the complexity of the system. At the same time, using inheritance to expand functions, we must foresee these extended functions. These functions are determined at compile time and are static.
Points: Decorators and the decorated have a common superclass. The purpose of inheritance is to inherit the type, not the behavior.
In fact Java The I/O API is implemented using Decorator.
Operation results:
In fact, it is a process of going into the house to find clothes and then finding maps, which enriches the details through the decorator's three-tier decoration.
Key points:
1. Decorator abstract class holds the Human interface, and all methods are delegated to the interface call. The purpose is to give the implementation class of the interface, i.e. subclass, to make the call.
2. Decorator abstract class subclass (concrete decorator), there is a constructor called super(human), this sentence reflects the principle that abstract class depends on subclass implementation, that is, Abstract depends on implementation. Because the parameters in the construction are all Human interfaces, so long as the implementation class of the Human can be passed in, it shows the structure of Decorator DT = new Decorator_second (new Decorator_first (new Decorator_zero (human)). So when dt.wearClothes();dt.walkToWhere() is called, and because each specific decorator class calls super.wearClothes and super.walkToWhere() methods first, and the super has been passed by the construct and pointed to a specific decorator class (this can be changed in order as needed), then the method of the decorator class is invoked, and then the method of the decorator class itself is invoked. Decorative method, that is, to show a decorative, chain-like behavior similar to filtering.
3. Specific decorated class, can define the initial state or the initial decoration of their own, the latter decorative behavior are based on this step by step to adorn, decoration.
4. The design principle of the decorator pattern is: open to expansion and close to modification. This sentence is reflected in the fact that if I want to expand the behavior of the decorated class, I need not modify the decorator Abstract class, only inherit the decorator Abstract class, and realize some additional decoration or behavior to package the decorated person. Therefore, expansion is embodied in inheritance and modification in subclasses rather than specific Abstract classes, which fully reflects the principle of inversion of dependence, which is the decorator model that I understand.
It's not clear what you're saying. Some people just feel that they can't express what they mean. Look at the code several times, then knock it out and run it on their own, and basically understand.
The following example also helps to understand the process and function of decoration.
Now you need a hamburger, the main body is chicken burger, you can choose to add lettuce, sauce, pepper and many other ingredients, in this case you can use the decorator mode.
Hamburg base class (decorated, equivalent to Hauman above)
Chicken Leg Castle (the initial state of the decorated person, some of their own simple decoration, equivalent to the above Person)
Base of ingredients (decorator, used to decorate hamburgers in multiple layers, adding some ingredients to each layer, equivalent to the above Decorator)
Lettuce (decorator_zero on the first floor of the decorator)
Chili pepper (decorator_first on the second floor of the decorator)
test class
output
Author: Jason 0539
Micro-blog: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (Reprinted please indicate the source)