By dynamically adding some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation. Dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.
The specific decorator and abstract decoration classes both inherit from the abstract decorator class and inherit from the type rather than behavior. Behavior comes from the decorator and basic components, or a combination with other decorator.
The decorator is generally created in a mode similar to a factory or generator.
Code Description
Package com. yydcdut. Decorate; // decored abstract class public interface phone {public void call ();}
Package com. yydcdut. Decorate; // public class realphone implements phone {@ override public void call () {system. Out. println... ");}}
Package COM. yydcdut. decorate; // modifier abstract class public abstract class phonedecorate implements phone {private phone = NULL; Public phonedecorate (phone) {This. phone = phone ;}@ override public void call () {This. phone. call ();}}
Package COM. yydcdut. decorate; // The modifier's specific class public class afterphone extends phonedecorate {Phone phone = NULL; Public afterphone (phone) {super (phone); this. phone = phone ;}@ override public void call () {super. call (); system. out. println ("after the call .... ");}}
Package COM. yydcdut. decorate; // The modifier's specific class public class beforephone extends phonedecorate {Phone phone = NULL; Public beforephone (phone) {super (phone); this. phone = phone ;}@ override public void call () {system. out. println ("before calling ..... "); Super. Call ();}}
Package COM. yydcdut. decorate; // test class, main function public class phonemain {public static void main (string [] ARGs) {Phone phone = new realphone (); phone. call (); system. out. println ("-----------------------"); phonedecorate Pd = new beforephone (phone); PD. call (); system. out. println ("-----------------------"); Pd = new afterphone (phone); PD. call (); system. out. println ("-----------------------"); Pd = new afterphone (New beforephone (phone); PD. call ();
}}
Summary
1) inheritance is one of the extensions, but it is not necessarily the best way to achieve elastic design.
2) in the design, the behavior should be allowed to be extended without modifying the existing code.
3) combination and delegation can be used to dynamically add new behaviors during runtime.
4) Apart from inheritance, the modifier mode also allows us to expand our behavior.
5) The decorator Mode means a group of decorator classes, which are used to wrap specific components.
6) The modifier class reflects the component type to be decorated (in fact, they have the same type and are implemented through interfaces or inheritance ).
7) the decorator Can add his or her own behavior before/or after the behavior of the decorator, and even replace the behavior of the decorator for a specific purpose.
8) You can package a component with countless decorators.
9) The decorator is generally transparent to the customer of the component, unless the customer program depends on the specific type of the component.
10) the decorator will lead to many small objects in the design. Excessive use will complicate the program.
The IO stream of Java also uses the decoration mode, bufferedinputstream/bufferedoutputstream/bufferedreader/buffereswriter, for example:
BufferedWriter bw = new BufferedWriter(new FileWriter(“abc.txt”));
I am the dividing line of tiantiao
Source code: http://pan.baidu.com/s/1dD1Qx01
Java .zip
Reprinted please indicate the source: http://www.cnblogs.com/yydcdut