Quote Encyclopedia
Decoration mode refers to the ability to dynamically extend an object without changing the original class file and using inheritance. It is by creating a wrapper object that is decorated to wrap the real object.
features
(1) Decorative objects and real objects have the same interface. This allows the client object to interact with the decorated object in the same way as the real object. (2) A decorative object containing a reference to a real object (reference) (3) decorates the object to accept all requests from the client. It forwards these requests to the real object. (4) Decorative objects can add additional functionality before or after forwarding these requests. This ensures that, at run time, additional functionality can be added externally without modifying the structure of the given object. In object-oriented design, a function extension to a given class is usually implemented through inheritance.
Working with scenes
1. You need to extend the functionality of a class or add additional responsibilities to a class. 2. You need to dynamically add functionality to an object that can be undone dynamically. 3. There is a need to increase the number of functions generated by the permutations and combinations of some basic functions, thus making the inheritance relationship impractical. 4. When it is not possible to expand using the method of generating subclasses. 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, making the number of subclasses explode. Another scenario might be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
Related Roles
Abstract Widget (Component) role: gives an abstract interface to the specification to prepare the object to receive additional responsibility.
Concrete Widget (concretecomponent) Role: defines a class that will receive additional responsibility.
Decorative (decorator) Role: holds an instance of a widget (Component) object and defines an interface that is consistent with the abstract component interface.
specific decoration (concretedecorator) Role: responsible for the component object "affixed" additional responsibilities.
Specific implementation:
Related code:
1. Abstract Role Interface
Public interface Component {public
void operation ();
}
2, abstract role specific implementation
public class Realcomponent implements component{
@Override public
void operation () {
System.out.println ("Concrete action ...");
}
3, decorate the role
public class Decorator implements Component {
private Component Component;
Public decorator (Component c) {
this.component = c;
}
@Override public
void operation () {
this.component.operation ();
}
}
4, decorate the role concretely realizes
public class Realdecoratora extends decorator {public
Realdecoratora (Component c) {
super (c);
}
@Override public
void operation () {
super.operation ();
System.out.println (".....) Custom handle A ... ");
5. Client-side test
public class Client {public
static void Main (string[] args) {
//TODO auto-generated method stub
Component c = new Realcomponent ();
Realdecoratora a = new Realdecoratora (c);
A.operation ();
}
A
The above simple decoration mode successfully implemented the need to add specific features Realdecoratora
Attached to the original implementation of the Realcomponent, after running the output
Concrete operation ...
... Custom Processing a ...
Advantages:
1. The purpose of the decorator mode and inheritance relationship is to extend the functionality of the object, but decorator can provide more flexibility than inheritance. 2. Through the use of different specific decorative classes and the arrangement of these decorative classes, designers can create a combination of many different behaviors. Disadvantages:
1. This more flexible nature than inheritance also means more complexity. 2. Decorative patterns can lead to many small classes in the design, which can complicate the program if used excessively.
Design principles:
1. Multi-use combination, less inheritance. The behavior of designing subclasses with inheritance is statically determined at compile time, and all subclasses inherit to the same behavior. However, if you can extend the behavior of an object by using a combination of practices, you can extend it dynamically at run time. 2. Classes should be designed for extended opening, for modifications to be closed.