Westward decorative pattern design mode ----

  Decorative patterns (Decorator): dynamically to an object to add some additional responsibilities, increased functionality, the decorative pattern is more flexible than a subclass.

 

 

 Code Example:

public abstract class Component {

public abstract void operation();

}

the extends the Component class ConcreteComponent {public 

@Override
public void Operation () {
System.out.println ( "specific operation object");
}
}

public class Decorator extends Component {

protected Component component;

public void setComponent(Component component) {
this.component = component;
}

@Override
public void operation() {
if(component != null){
component.operation();
}
}
}

public class ConcreteDecoratorA extends Decorator {

private String addedState;

@Override
public void operation(){
super.operation();
addedState = "New State";
System.out.println("具体装饰对象A的操作");
}
}

the extends the Decorator class ConcreteDecoratorB {public 

@Override
public void Operation () {
super.operation ();
addedBehavior ();
System.out.println ( "specific operation decorative object B");
}

public void addedBehavior () {
the System.out .println ( "additional operations decorative object B");
}

}

public class TestDecorator {

public static void main(String[] args) {
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA a = new ConcreteDecoratorA();
ConcreteDecoratorB b = new ConcreteDecoratorB();

c.operation();
System.out.println("--------------------------");
a.setComponent(c);
a.operation();
System.out.println("--------------------------");
b.setComponent(a);
b.operation();
System.out.println("--------------------------");
}
}

  If only one ConcreteComponent class without abstract Component class, Decorator class is a subclass of ConcreteComponent.

Similarly, if there is only one ConcreteDecorator class, then there is no need for a separate Decorator class, and can merge Decorator responsibility and ConcreteDecorator into a class.

Decorative pattern is to use SetComponent packaging object. Implementation of each decorative objects on and how to use this object separated, and each decorative objects only care about their function, do not need to care about how the object is added to the chain of them.

to sum up:

  Decorative pattern is there a way to add functionality dynamically more features. When the system needs to add new features when it is added to the old class in the new code, the new code is usually decorated with original class core responsibilities of key actors or adding a new logic, thereby increasing the complexity of the main class . Decorative pattern provides a very good solution, to decorate it to each function in a separate class, and make it to decorate the packaging of objects, so when you need to perform special acts, client code can at runtime required selectively, sequentially using the wrapped object decorative function.

       Benefit is effectively the core duties and decorative ribbon class apart. And related classes can remove duplicate decorative logic.

 

Guess you like

Origin www.cnblogs.com/zsmcwp/p/11622754.html