装饰模式学习

背景

首先明确装饰模式是结构型设计模式的一种,但是结构型设计模式有什么特点呢。装饰模式的业务是给人穿衣服。

步骤

历史发展

版本1:只有一个Person类,这个类由三部分构成,本身的有参构造函数,给当前对象传不同衣服的几个方法,还有一个展示穿衣服客体名称的方法。

在这里插入图片描述

public class Person {String  name;public  Person (String  name){this.name = name;}public  void wearTshirt(){System.out.println("穿上大背心");}public  void wearTrousers(){System.out.println("穿上大裤子");}public  void   show(){System.out.println("展示的是"+this.name);}
}
public class Client {public static void main(String[] args) {Person  person  = new Person("小菜");person.wearTrousers();person.wearTshirt();person.show();}
}

在这里插入图片描述

缺点:
增加超人装扮需要修改代码,不符合开闭原则。

改变:抽象出来装扮类,通过增加子类的方式添加新的装扮。

版本2:抽象出来人类和服饰类,还有客户端。(人和服饰类之间没有直接的依赖关系)

在这里插入图片描述

public class Client {public static void main(String[] args) {Person person  = new Person("小菜");Finery tshirt = new Tshirt();Finery trousers = new Trousers();trousers.show();tshirt.show();person.show();}
}
public   abstract class Finery {abstract   void   show();
}
public class Person {String  name;public  Person (String  name){this.name = name;}public  void   show(){System.out.println("展示的是"+this.name);}
}
public class Trousers extends  Finery{void show() {System.out.println("穿上裤子");}
}
public class Tshirt extends  Finery{void show() {System.out.println("穿上大背心");}
}

展示结果:
在这里插入图片描述
缺点:客户端知道的太多了,整个穿衣服的过程完完全全展示在了客户端里边,能不能在内部组装完毕,然后展示出来。

改进:能不能把所需要的功能按照正确的顺序串联起来进行控制。

版本3:引入装饰模式

在这里插入图片描述

在这里插入图片描述

public class Client {public static void main(String[] args) {Person person  = new Person("小菜");InnerFinery  innerFinery = new InnerFinery();Finery tshirt = new Tshirt();Finery trousers = new Trousers();innerFinery.decorate(person);tshirt.decorate(innerFinery);trousers.decorate(tshirt);trousers.show();}
}
public    class Finery   extends Person {private Person  component;public void decorate (Person  component){this.component = component;}public    void   show(){if(component != null){component.show();}};
}
public class InnerFinery  extends Finery {public  void show(){System.out.println("穿内裤");super.show();}}
public class Person {public Person(){};String  name;public  Person (String  name){this.name = name;}public  void   show(){System.out.println("展示的是"+this.name);}
}
public class Trousers extends Finery {public void show() {System.out.println("穿上裤子");super.show();}
}
public class Tshirt extends Finery {public void show() {System.out.println("穿上大背心");super.show();}
}

在这里插入图片描述

应用

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

在第三版代码中,这个对象是Person对象,额外的职责就是具体的装饰子类,但是person类并不知道Finery类。

放到我们的业务中就是主类不知道要附加类的存在,但是附加类知道主类的存在。

特点

装饰模式是为已有的功能动态添加更多功能的一种方式(这里是不是可以浅薄的理解为原来需要增加子类的那种都可以使用装饰模式实现呢)

和别的设计模式进行对比

这里可以和建造者模式进行类比,添加部件的顺序是在Decorator里边定义好的。但是装饰模式这里穿衣服的顺序是可以随时变化的,不适合直接写死在代码里边,所以基于当前这穿衣服的业务,不适合使用建造者和模板方法,模板方法里边也是将方法执行的步骤在源码就写好了。

总结

1、书上的超人是谁?是ConcreteComponent类的子类还是和这个类是同一层级的???

2、图上的聚合关系是如何进行体现的
3、哪里叫内部组装?
4、实现和使用分离式如何体现的呢???