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 a structured pattern .
structureFigure-Decorator mode
Component: Defines an object interface that can dynamically add responsibilities to these objects.InterfaceComponent {
PublicvoidOperation ();
}
concretecomponent: Defines a specific object, or adds some responsibility to the object.classConcretecomponentImplementsComponent {
@Override
PublicvoidOperation () {
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.classDecoratorImplementsComponent {
//hold a Component object, and Component form an aggregation relationship
protectedComponent Component;
//passing in objects to be further decorated
PublicDecorator (Component Component) {
This. Component = component;
}
@Override
//calling the original method to decorate the object
Public voidOperation () {
Component.operation ();
}
}
Concretedecorator: The specific Decoration object, plays the function which adds the responsibility to the Component.classConcretedecoratoraextendsDecorator {
PrivateString addedstate = "new attribute 1";
PublicConcretedecoratora (Component Component) {
Super(component);
}
PublicvoidOperation () {
Super. Operation ();
SYSTEM.OUT.PRINTLN ("Add attribute:" + addedstate);
}
}
classConcretedecoratorbextendsDecorator {
PublicConcretedecoratorb (Component Component) {
Super(component);
}
PublicvoidOperation () {
Super. Operation ();
Addedbehavior ();
}
PublicvoidAddedbehavior () {
SYSTEM.OUT.PRINTLN ("Add Behavior");
}
}
Test code
PublicclassDecoratorpattern {
Public StaticvoidMain (string[] args) {
Component Component =NewConcretecomponent ();
Component.operation ();
System.out.println ("======================================");
Decorator Decoratora =NewConcretedecoratora (component);
Decoratora.operation ();
System.out.println ("======================================");
Decorator Decoratorb =NewConcretedecoratorb (Decoratora);
Decoratorb.operation ();
}
}
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