学习大话设计模式06_装饰模式

装饰模式(Decorator):

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

 

 1 /**
 2  * Component 是定义一个对象接口,可以给这些对象动态地添加职责
 3  * @author Monica
 4  *
 5  */
 6 public abstract class Component {
 7     public abstract void Operation();
 8 }
 9 
10 
11 /**
12  * ConcreteComponent 是定义了一个具体的对象,也可以给这个对象添加一些职责
13  * @author Administrator
14  *
15  */
16 public class ConcreteComponent extends Component{
17 
18     @Override
19     public void Operation() {
20         System.out.println("具体对象的操作");
21     }
22 }
23 
24 /**
25  * Decorator 装饰抽象类,继承了Component,从扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的
26  * @author Administrator
27  *
28  */
29 public class Decorator extends Component {
30     private Component component;
31     
32     //设置Component
33     public void setComponent(Component component) {
34         this.component = component;
35     }
36     
37     @Override
38     public void Operation() {
39         //重写Operation(), 实际执行的是Component的Operation()
40         if(component != null) {
41             component.Operation();
42         }
43     }
44  
45 }
46 
47 class ConcreteDecoratorA extends Decorator {
48     //本类的独有功能,以区别于ConcreteDecoratorB
49     private String addedState;
50     
51     @Override
52     public void Operation() {
53         //首先运行原Component的Operation(),再执行本类的功能,如addedState,相当于对原Component进行了装饰
54         super.Operation();
55         addedState = "New State";
56         System.out.println("具体装饰对象A的操作");
57     }
58 }
59 
60 class ConcreteDecoratorB extends Decorator {
61     
62     @Override
63     public void Operation() {
64         //首先运行原Component的Operation(),再执行本类的功能,如addedState,相当于对原Component进行了装饰
65         super.Operation();
66         AddedBehavior();
67         System.out.println("具体装饰对象B的操作");
68     }
69 
70     private void AddedBehavior() {
71         
72     }
73 }

执行方法:

public static void main(String[] args) {
        ConcreteComponent c = new ConcreteComponent();
        ConcreteDecoratorA d1 = new ConcreteDecoratorA();
        ConcreteDecoratorB d2 = new ConcreteDecoratorB();
        /*
         * 装饰的方法是:
         * 首先用ConcreteComponent实例化对象c,
         * 然后用ConcreteDecoratorA的实例化对象d1来包装c,
         * 再用ConcreteDecoratorB的对象d2包装d1,
         * 最后执行d2的Operation()
         */
        d1.setComponent(c);
        d2.setComponent(d1);
        d2.Operation();
}

 

装饰模式是利用SetComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

如何只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

 

/**
 * Person类(ConcreteComponent)
 * @author Administrator
 *
 */
class Person {
    public Person() {
        
    }
    
    private String name;
    
    public Person(String name) {
        this.name = name;
    }
    
    public void Show() {
        System.out.println("装扮的" + name);
    }
}

/**
 * 服饰类(Decorator)
 * @author Administrator
 *
 */
class Finery extends Person {
    protected Person component;
    
    //打扮
    public void Decorate(Person component) {
        this.component = component;
    }
    
    @Override
    public void Show() {
        if(component != null) {
            component.Show();
        }
    }
}

/**
 * 具体服饰类(ConcreteDecorator)
 * @author Administrator
 *
 */
class TShirts extends Finery {
    
    @Override
    public void Show() {
        System.out.println("大T恤");
        super.Show();
    }
}

class BigTrouser extends Finery {
    
    @Override
    public void Show() {
        System.out.println("垮裤");
        super.Show();
    }
}

//其余类类似,省略

调用方法:

public static void main(String[] args) {
        Person xc = new Person("小菜");
        
        BigTrouser kk = new BigTrouser();
        TShirts dtx = new TShirts();
        
        kk.Decorate(xc);
        dtx.Decorate(kk);
        dtx.Show();
    }

 

posted @ 2014-08-21 15:07  QqiaoQ  阅读(137)  评论(0编辑  收藏  举报