你的位置:首页 > Java教程

[Java教程]装饰器设计模式初探(Java实现)


  本篇随笔主要介绍用Java实现简单的装饰器设计模式:

    先来看一下装饰器设计模式的类图:

    

     从图中可以看到,我们可以装饰Component接口的任何实现类,而这些实现类也包括了装饰器本身,装饰器本身也可以再被装饰。

 

    下面是用Java实现的简单的装饰器设计模式,提供的是从基本的加入咖啡入手,可以继续加入牛奶,巧克力,糖的装饰器系统。

    

 1 interface Component { 2   void method(); 3 } 4 class Coffee implements Component { 5  6   @Override 7   public void method() { 8     // TODO Auto-generated method stub 9     System.out.println("倒入咖啡");10   }11   12 }13 class Decorator implements Component {14   public Component comp;15   public Decorator(Component comp) {16     this.comp = comp;17   }18   @Override19   public void method() {20     // TODO Auto-generated method stub21     comp.method();22   }23   24 }25 class ConcreteDecorateA extends Decorator {26   public Component comp;27   public ConcreteDecorateA(Component comp) {28     super(comp);29     this.comp = comp;30   }31   public void method1() {32     System.out.println("倒入牛奶");33   }34   public void method2() {35     System.out.println("加入糖 ");36   }37   public void method() {38     super.method();39     method1();40     method2();41   }42 }43 class ConcreteDecorateB extends Decorator {44   public Component comp;45   public ConcreteDecorateB(Component comp) {46     super(comp);47     this.comp = comp;48   }49   public void method1() {50     System.out.println("加入巧克力");51   }52   public void method() {53     super.method();54     method1();55   }56 }57 public class TestDecoratePattern {58   public static void main(String[] args) {59     Component comp = new Coffee();60     comp.method();61     System.out.println("--------------------------------------------------");62     Component comp1 = new ConcreteDecorateA(comp);63     comp1.method();64     System.out.println("--------------------------------------------------");65     Component comp2 = new ConcreteDecorateB(comp1);66     comp2.method();67     System.out.println("--------------------------------------------------");68     Component comp3 = new ConcreteDecorateB(new ConcreteDecorateA(new Coffee()));69     comp3.method();70     System.out.println("--------------------------------------------------");71     Component comp4 = new ConcreteDecorateA(new ConcreteDecorateB(new Coffee()));72     comp4.method();73   }74 }

    

    运行结果:

    

 

    装饰器设计模式可以使得我们自由的,以任意顺序导入巧克力,牛奶,咖啡和糖。可以实现多层,任意顺序的装饰。真是牛~