JAVA Design Pattern Decorator mode
Use
Decorator Mode (Decorator) dynamically adds Some additional responsibilities to an object. For added functionality, the Decorator mode is more flexible than generating subclasses.
Decorator Mode is aStructural mode。
structure figure-Decorator pattern structure diagram
Component: Defines an object interface that can dynamically add responsibilities to these objects.Interface Component {
public void operation ();
}concretecomponent: ImplementComponentDefines the interface.class Concretecomponent implements Component {
@Override
public void operation () {
SYSTEM.OUT.PRINTLN ("initial behavior");
}
}Decorator: Decorate abstract class, inherit Component, extend the function of Component class from outer class, but for Component, there is no need to know the existence of Decorator.Class DecoratorImplements Component {
//Hold a Component object, and Component form an aggregation relationship
protected Component Component;
passing in objects to be further decorated
Public Decorator (Component Component) {
this.component = component;
}
@Override
// Call the original method to decorate the object
public void operation () {
Component.operation ();
}
}Concretedecorator: The specific Decoration object, plays the function which adds the responsibility to the Component.Class ConcretedecoratoraExtends Decorator {
Private String addedstate = "new attribute 1";
Public Concretedecoratora (Component Component) {
Super (component);
}
Publicvoid operation () {
Super.operation ();
SYSTEM.OUT.PRINTLN ("Add attribute:" + addedstate);
}
}
Class Concretedecoratorbextends decorator {
public concretedecoratorb (component component) {
super (component);
}
public void operation () {
super.operation ();
addedbehavior ();
}
public void addedbehavior () {
system.out.println ("Add Behavior" );
}
}
Test code
PublicClass Decoratorpattern {
PublicStaticvoid Main (string[] args) {
Component Component =new concretecomponent ();
component.operation ();
System.out.println ("======================================");
Decorator decoratorA = new concretedecoratora (component);
decoratora.operation ();
System.out.println ("======================================");
Decorator decoratorB = new concretedecoratorb (Decoratora);
decoratorb.operation ();
}
}
Run results
Initial behavior
======================================
Initial behavior
Add Property: New Attribute 1
======================================
Initial behavior
Add Property: New Attribute 1
Add behavior
Application Scenarios 1, need to dynamically, transparently add responsibility for an object, that is, do not affect other objects.
2, the need to dynamically add functionality to an object, these functions can be re-dynamic revocation.
3. It is necessary to increase the number of functions produced by the permutation and combination of some basic functions, so that the inheritance relationship becomes unrealistic.
4, when the method of generating subclasses can not be expanded. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
Key points
1, decorative objects and real objects have the same interface. This allows the client object to interact with the adornment object in the same way as the real object.
2. The Adornment object contains a reference to a real object (reference).
3. The Adornment object accepts all requests from the client. It forwards these requests to the real object.
4. Decorative objects can add some additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance.
Java design pattern-decorator mode