Decorator

定义

  装饰(Decorator)模式又名包装模式,以对客户透明的方式动态地将责任附加到对象上.它是继承关系的一个替代方案。

 

结构和角色

  

  抽象构件(Component) 角色 : 给出一个抽象接口,规范其具体构件的行为。 

  具体构件(ConcreteComponent)角色 : 被装饰的类,(源对象)。

  装饰(Decorator)角色 : 持有一个构件角色的实例, 并实现抽象构建接口。

  具体修饰(ConcreteDecorator)角色 :  负责给源对象动态加上附加功能。

注意

  装饰者和被装饰者对象有相同的超类型,在任何需要源对象(被装饰者)的场合都可以用装饰过的对象代替它。

  可以用一个或多个装饰者包装对象,因为装饰者的实例变量是一个最上层接口(Component)。

  装饰者可以在所委托被装饰的行为之前或之后,加上自己的行为,以达到特定的目的。

  对象可以在任何时候被装饰,所以可以运行时动态,不限量地增加装饰类。

 

  

public interface Component {
    void operation();
}
public class ConcreteComponent implements Component{

    @Override
    public void operation() {
        System.out.println("This is concrete component");
    }

}
public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }
    
    @Override
    public void operation() {
        component.operation();    
    }
    
}
public class ConcreteDecorator extends Decorator{

    public ConcreteDecorator(Component component) {
        super(component);
    }
    
    @Override
    public void operation() {
        System.out.println("add other function before ");
        
        super.operation();
        
        System.out.println("add other function after ");
    }
    
}
public class ConcreteDecorator2 extends Decorator{

    public ConcreteDecorator2(Component component) {
        super(component);
    }
    
    @Override
    public void operation() {
        System.out.println("add other function before 22");
        
        super.operation();
        
        System.out.println("add other function after 222");
    }
    
    
}
public class Test {
    public static void main(String[] args) {
        
        Component com = new ConcreteComponent();
        com = new ConcreteDecorator(com);
        com = new ConcreteDecorator2(com);
        
        com.operation();
        
    }
}

  add other function before 22
  add other function before
  This is concrete component
  add other function after
  add other function after 222

 

  继承的问题(关于继承写一篇总结):

    类的行为只能在编译时静态决定。

  继承的目的在于建立一种关系,而不是为了复用上层的行为。

 

使用情景

  需要扩展一个类的功能,或给一个类增加附加责任。

  需要动态地给一个对象增加功能,这些功能可以再动态地撤销。

  需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变得不现实。

实例

  Java I/O 的类设计是装饰模式的一个典型应用。 Java  I/O的类结构主要由 装饰模式和适配器模式构成。(学完适配器模式,写一篇总结Java I/O中的模式,和比较装饰模式和适配器模式)

  

优缺点

  装饰模式与继承关系的目的都是要扩展对象的功能,但是装饰模式可以提供比继承更多的灵活性。

  装饰类可以使用排列组合

缺点

  增加灵活性的同时也更容易出错。差错变得困难,因为这些对象看上去都很类似。

 

背后的设计原则

  装饰模式的核心原则是遵循开闭原则,(对扩展开放,对修改关闭)。装饰类就是一种扩展,这种动态的方式不会对源对象产生影响,且对客户端来说是透明的。

 

posted @ 2014-03-04 22:13  廖东海  阅读(224)  评论(0编辑  收藏  举报