Decorator Mode
Dynamically attaches the responsibility to the object. To extend functionality, Decorator mode provides a more resilient alternative than inheritance.
Description:1, the decorator and the object of the adorner have the same super-type; 2, you can use one or more decorators to wrap an object, 3, since the decorator and the object of the adorner has the same super-type, so in any need of the original object (the decorator), can be used to decorate objects instead of it;
4. The decorator may, before and/or after the act of entrusting the decorator, add his or her actions to achieve a specific purpose5. Objects can be decorated at any time, so you can decorate objects dynamically and without limits at runtime with your favorite decorators.
The roles in the decorating mode are:
abstract Component (Component) role : An abstract interface is given to standardize the objects that are ready to receive additional responsibilities.
Concrete Component (concretecomponent) role : Defines a class that will receive additional responsibilities.
decoration (Decorator) role : Holds an instance of a component (Component) object and defines an interface that is consistent with the abstract component interface.
specific decoration (concretedecorator) role : Responsible for attaching the Component object "affixed" to the responsibility.
| /** * Monkey King's Buddha, and defines the method of 72 change. * Abstract Component (Component) role */public abstract class Monkey {protected String type = "Buddha"; protected abstract void change (); Public String GetType () {return type; }} |
| /** * Franzia Tianda * Concrete Component (concretecomponent) role * */public class monkeyking extends monkey{ public monkeyking () { type = "Qi Tianda -> "; } @Override protected void change () { system.out.println ("72 changes ..."); } |
| /** * change of Monkey King *& nbsp Decoration (Decorator) Role * */public abstract class changemonkey extends monkey{ @Override protected abstract void change () ; } |
| /** * changed to fish * Concrete Decoration (concretedecorator) character * */public class fish extends changemonkey{ private monkey monkey; public fish ( Monkey monkey) { this.monkey = monkey; } @Override protected void change () { system.out.println ("Change to Fish ... "); } public string gettype () { return monkey.gettype () + "fish -> "; }} |
| /** * changed to Bird * Concrete Decoration (concretedecorator) character * */public class bird extends changemonkey{ private monkey monkey; public bird ( Monkey monkey) { this.monkey = monkey; } @Override protected void change () { system.out.println ("Bird ..."); } public string gettype () { return monkey.gettype () + "bird -> "; }} |
| public class Client {public static void main (string[] args) {Monkey Monkey = new monkeyking (); Monkey fish = new Fish (Monkey); Monkey fish2 = new Fish (fish); Monkey bird = new Bird (FISH2); System.out.println (Bird.gettype ()); Bird.change (); } } |
application of decorative patterns in Java I/O
As you can see:
abstract Component (Component) role : Played by InputStream. This is an abstract class that provides a unified interface for each seed type.
specific component (concretecomponent) roles : Bytearrayinputstream, FileInputStream, PipedInputStream, StringBufferInputStream and other class play. They implement the interfaces specified by the abstract component role.
abstract Decoration (Decorator) role : Played by FilterInputStream. It implements the interface specified by the InputStream.
specific decoration (concretedecorator) role : Played by several classes, namely Bufferedinputstream, DataInputStream and two infrequently used classes Linenumberinputstream, Pushbackinputstream.
Advantages of Decorative mode
(1) The purpose of the adornment mode and the inheritance relation is to extend the function of the object, but the adornment mode can provide more flexibility than the inheritance. Decoration mode allows the system to dynamically decide to "paste" a desired "decoration", or to remove an unwanted "decoration". The inheritance relation is different, the inheritance relation is static, it decides before the system runs.
(2) Designers can create a combination of many different behaviors by using different decorative classes and arranging combinations of these decorations.
Disadvantages of decoration mode
Because of the use of adornment mode, a smaller number of destination classes are required than using inheritance relationships. The use of fewer classes, of course, makes the design easier to carry out. However, on the other hand, using the adornment pattern produces more objects than using the inheritance relationship. More objects make it difficult to find errors, especially when they look alike.
Reference: "Head first design mode" http://www.cnblogs.com/java-my-life/archive/2012/04/20/2455726.html
Decorator pattern for design pattern Learning notes