First, Mode analysis
Decorative mode is also known as packaging (Wrapper) mode. Adornment mode extends the functionality of the object transparently to the client, and is an alternative to the inheritance relationship.
The main points of the decoration mode are:
1, need to extend new functions to existing objects, and do not want to change the original object interface;
2, the adorner object and the original object need to inherit the same interface, the original object is initialized when the objects are imported into the decorative object;
3, can be defined on an object multiple decorative objects, to be individually decorated or combined decoration
Second, the mode code
1. Abstract interface
Package Decorator.patten; Public Interface Component { publicvoid operation ();}
2. Define the object to be decorated
Package Decorator.patten; Public class Implements Component { @Override publicvoid operation () { System.out.println ( "I was decorated object, I executed");} }
3. Define Decorator Object A
Package Decorator.patten; Public class Implements Component { Component Component; Public Decoratora (Component Component) { this.component=Component; } @Override publicvoid operation () { System.out.println ("I am decorating object A, I added print before being decorated object "); Component.operation (); }}
4. Define Decorator Object B
Package Decorator.patten; Public class Implements Component { Component Component; Public Decoratorb (Component Component) { this.component=Component; } @Override publicvoid operation () { component.operation (); System.out.println ("I am decorating object B, I am adding print after being decorated object");} }
5. Define Client
Public class Client { publicstaticvoid main (string[] args) { Component Component =new decoratorb ( new Decoratora (new concretecomponent ())); Component.operation (); }}
6. Implementation results
I was decorating object A, I added print before being decorated object I was decorated object, I performed I was decorating object B, I added print after being decorated object
Third, the description
1, decorative mode can be decorated to add new features, the core functions and decorative functions of the separation, such as log printing, character set processing, etc.
2, the object can be more decorative, decoration will be executed
3, the example of multiple decorations do not order, but in practice often will be orderly, such as data encryption and data filtering, if the first encryption and filtering will be problematic
4, decorative objects and decorative objects are integrated with the same interface, and sometimes in order to simplify, we will be decorative objects directly integrated into the decorated object, this is the subclass overriding the parent class method to achieve the extended function
Iv. Application Scenarios
The most typical application of decorative mode is the InputStream and outputstream in Java IO, such as
New DataInputStream (New bufferedinputstream (new fileinputstream (" Test.txt "))) ;
[design mode at work] Decorative mode decorator