Decorator Pattern
character:the original object's function is OK,but can not be used into new situation.so it needs additional opearions.
the new object with something new and includes the original object is called decorator.
as to the realization,it can be carried out in three steps:
1.the original object with some basic functions
1.1 abstract object
public abstract class Component {
public abstract void operation();
}
1.2 concrete object
public class ConcreteCompment extends Component{
public void operation(){
System.out.println("It's in operation.");
}
}
2.the decorator object with additional functions
2.1 abstract decorator(define the operation)
public class Decorator {
protected Component component;
public Decorator(Component component){
this.component=component;
}
public void operation(){
component.operation();
}
}
2.2 concrete decorator(add additional operations)
public class ConcreteDecorator extends Decorator{
public ConcreteDecorator(Component component){
super(component);
}
private void operationA(){
System.out.println("It's in operationA.");
}
private void operationZ(){
System.out.println("It's in operationZ.");
}
public void operation(){
operationA();
super.operation();
operationZ();
}
}
3.the usage of decorator
public class Client {
public static void main(String[] args) {
Component custom=new ConcreteCompment();
Decorator decorator=new ConcreteDecorator(custom);
decorator.operation();
}
}