简单说说装饰模式

这里只简单说说装饰模式,因为对这个模式理解得还不是很透彻。暂时不能写出更深一点的内容。

什么是装饰模式呢?拟定一个场景,一个人需要穿衣打扮,我们可以写一个Person类,为它定义出N个方法,穿短袖,穿皮鞋,等等。要新增一个装饰的时候,我们在Person类里新增一个方法即可,但这违背了“方法-封闭”原则。如何写出更好更加灵活的代码呢?我们用装饰模式来实现这个场景。小明先穿短袖,后穿皮鞋。

穿短袖、穿皮鞋等等,我们把这些装饰方法干脆抽象出来成一个装饰类,不同的装饰继承实现这个类。人定义为一个接口,小明是一个具体的人继承“人”接口。同时装饰类聚合并且实现“人”接口,这点也是我暂时没法深入理解的问题所在。有点绕口,我们还是先画出UML类图。

清楚类结构后,我们照着类结构来实现代码。

首先定义Component抽象构件(装饰类装饰的是抽象构件)接口。

 1 package day_6_decorator;
 2 
 3 /**
 4  * 抽象构件
 5  * @author turbo
 6  *
 7  * 2016年9月9日
 8  */
 9 public interface Component {    //人
10     void operation();
11 }

实现这个Component抽象构件,创建一个ConcreteComponent具体构件。

 1 package day_6_decorator;
 2 
 3 /**
 4  * 具体构件
 5  * @author turbo
 6  *
 7  * 2016年9月9日
 8  */
 9 public class ConcreteComponent implements Component {    //小明
10 
11     /* (non-Javadoc)
12      * @see day_6_decorator.Component#Operation()
13      */
14     @Override
15     public void operation() {
16         System.out.println("具体对象的操作");
17     }
18 
19 }

定义装饰类。

 1 package day_6_decorator;
 2 
 3 /**
 4  * 装饰类
 5  * @author turbo
 6  *
 7  * 2016年9月9日
 8  */
 9 public class Decorator implements Component {
10     private Component component;
11     
12     public void setComponent(Component component) {
13         this.component = component;
14     }
15     /* (non-Javadoc)
16      * @see day_6_decorator.Component#Operation()
17      */
18     @Override
19     public void operation() {
20         if (component != null){
21             component.operation();
22         }
23     }
24 
25 }

定义具体装饰类ConcreteDecorateA。

 1 package day_6_decorator;
 2 
 3 /**
 4  * 具体装饰类A
 5  * @author turbo
 6  *
 7  * 2016年9月10日
 8  */
 9 public class ConcreteDecoratorA extends Decorator {
10     private String addedState;    //本类独有功能
11     
12     @Override
13     public void operation() {
14         super.operation();
15         addedState = "new State";
16         System.out.println("穿短袖");
17     }
18     
19 }

定义具体装饰类ConcreteDecorateB。

 1 package day_6_decorator;
 2 
 3 /**
 4  * 具体装饰类B
 5  * @author turbo
 6  *
 7  * 2016年9月10日
 8  */
 9 public class ConcreteDecoratorB extends Decorator {
10 
11     @Override
12     public void operation() {
13         super.operation();
14         addedBehavior();
15     }
16 
17     /**
18      * 
19      */
20     private void addedBehavior() {
21         System.out.println("穿皮鞋");
22     }
23     
24 }

客户端代码。

 1 package day_6_decorator;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月10日
 7  */
 8 public class Main {
 9 
10     /**
11      * @param args
12      */
13     public static void main(String[] args) {
14         ConcreteComponent c = new ConcreteComponent();
15         ConcreteDecoratorA d1 = new ConcreteDecoratorA();
16         ConcreteDecoratorB d2 = new ConcreteDecoratorB();
17         
18         d1.setComponent(c);
19         d2.setComponent(d1);
20         d2.operation();
21     }
22 
23 }

输出结果。

这样我们就用两个装饰类对小明做了装饰,并且可以随意调换装饰的顺序。暂时还不能深入地聊下装饰模式,待我理解比较透彻再来深入理解下,不过这个装饰模式好像和Java的动态代理机制倒是有几分相似。同样在之后也会仔细研究研究。

原文地址:https://www.cnblogs.com/yulinfeng/p/5858527.html