Purpose: Expand the object function.
Involved roles:
1. Abstract component role: defines an abstract interface to standardize the classes for preparing additional functions.
2. Specific Component role: class to be attached with a function to implement the abstract component role interface.
3. Abstract modifier role: holds a reference to a specific component role and defines an interface consistent with the abstract component role.
4. Specific decoration role: implements the abstract decoration role and adds additional functions to specific components.
Code example:
Java code of the abstract component role
package decorator;
public interface InterfaceComponent {
public void say();
}
Java code of the specific component role
package decorator;
public class Component implements InterfaceComponent{
public void say() {
System. Out. println ("component. Say (): Method of the original component! ");
}
}
Java code of the abstract modifier role
package decorator;
public abstract class AbstractDecorator implements InterfaceComponent{
private InterfaceComponent component;
public AbstractDecorator(InterfaceComponent component){
this.component = component;
}
Protected void presay () {}; // pre-processing method before the component method is executed
Protected void aftersay () {}; // post-processing method for component method execution
public void say(){
preSay();
component.say();
afterSay();
};
}
Specific modifier 2 Java code
package decorator;
public class DecoratorTwo extends AbstractDecorator{
public DecoratorTwo(InterfaceComponent component) {
super(component);
}
Protected void presay () {// reload the template class presay () method as needed
System. Out. println ("decoratortwo. presay (): presay () method of modifier 2! ");
}
Protected void aftersay () {// reload the template class aftersay () method as needed
System. Out. println ("decoratortwo. aftersay (): The aftersay () method of modifier 2! ");
}
}
Modifier 1 Java code
package decorator;
public class DecoratorOne extends AbstractDecorator{
public DecoratorOne(InterfaceComponent component) {
super(component);
}
Protected void presay () {// reload the template class presay () method as needed
System. Out. println ("decoratorone. presay (): presay () method of modifier 1! ");
}
Protected void aftersay () {// reload the template class aftersay () method as needed
System. Out. println ("decoratorone. aftersay (): The aftersay () method of modifier 1! ");
}
Public static void main (string [] ARGs) {// Test Method
InterfaceComponent interfaceComponent = new DecoratorTwo(new DecoratorOne(new Component()));
interfaceComponent.say();
}
}
Console output:
-
* Console output:
-
* Decoratortwo. presay (): The presay () method of decoratortwo!
-
* Decoratorone. presay (): The presay () method of decoratorone 1!
-
* Component. Say (): Method of the original component!
-
* Decoratorone. aftersay (): The aftersay () method of modifier 1!
-
* Decoratortwo. aftersay (): The aftersay () method of decoratortwo!
4. Advantages and Disadvantages
Advantages: 1) provide more flexibility than inheritance 2) use different decorative combinations to create combinations of different behaviors 3) Reduce the number of classes required
Disadvantages: 1) Flexibility leads to greater errors. 2) more objects are generated, which makes error detection difficult.