经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
大话设计模式笔记(四)の装饰模式
来源:cnblogs  作者:callmeDevil  时间:2019/6/24 8:53:43  对本文有异议

举个栗子

问题描述

可以给人搭配嘻哈服或白领装的程序。

简单实现

代码

  1. /**
  2. * 人类
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class Person {
  6. private String name;
  7. public Person(String name) {
  8. this.name = name;
  9. }
  10. public void wearTShirts(){
  11. System.out.print("大T恤 ");
  12. }
  13. public void wearBigTrouser(){
  14. System.out.print("垮裤 ");
  15. }
  16. public void wearSneakers(){
  17. System.out.print("破球鞋 ");
  18. }
  19. public void wearSuit(){
  20. System.out.print("西装 ");
  21. }
  22. public void wearTie(){
  23. System.out.print("领带 ");
  24. }
  25. public void wearLeatherShoes(){
  26. System.out.print("皮鞋 ");
  27. }
  28. public void show(){
  29. System.out.println("装扮的" + name);
  30. }
  31. }
  1. /**
  2. * 装扮测试类
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class Test {
  6. public static void main(String[] args) {
  7. Person devil = new Person("Devil");
  8. System.out.println("第一种装扮:");
  9. devil.wearTShirts();
  10. devil.wearBigTrouser();
  11. devil.wearSneakers();
  12. devil.show();
  13. System.out.println("\n第二种装扮:");
  14. devil.wearSuit();
  15. devil.wearTie();
  16. devil.wearLeatherShoes();
  17. devil.show();
  18. }
  19. }

测试结果

  1. 第一种装扮:
  2. T 垮裤 破球鞋 装扮的Devil
  3. 第二种装扮:
  4. 西装 领带 皮鞋 装扮的Devil

存在缺陷

如果需要增加“超人”装扮,会导致需要修改“Person”类,违背了开放-封闭原则

简单实现进化版

代码

  1. /**
  2. * 人类
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class Person {
  6. private String name;
  7. public Person(String name){
  8. this.name = name;
  9. }
  10. public void show(){
  11. System.out.print("装扮的" + name);
  12. }
  13. }
  1. /**
  2. * 服装抽象类
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public abstract class Finery {
  6. public abstract void show();
  7. }
  1. /**
  2. * T恤 类
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class TShirts extends Finery {
  6. @Override
  7. public void show() {
  8. System.out.print("大T恤 ");
  9. }
  10. }
  1. /**
  2. * 垮裤 类
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class BigTrouser extends Finery {
  6. @Override
  7. public void show() {
  8. System.out.print("垮裤 ");
  9. }
  10. }
  11. // 其余子类相似,此处省略
  1. /**
  2. * 装饰升级版测试
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class Test {
  6. public static void main(String[] args) {
  7. Person devil = new Person("Devil");
  8. System.out.println("第一种装扮:");
  9. Finery tShirts = new TShirts();
  10. Finery bigTrouser = new BigTrouser();
  11. Finery sneakers = new Sneakers();
  12. tShirts.show();
  13. bigTrouser.show();
  14. sneakers.show();
  15. devil.show();
  16. System.out.println("\n第二种装扮:");
  17. Finery suit = new Suit();
  18. Finery tie = new Tie();
  19. Finery leatherShoes = new LeatherShoes();
  20. suit.show();
  21. tie.show();
  22. leatherShoes.show();
  23. devil.show();
  24. }
  25. }

测试结果

  1. 第一种装扮:
  2. T 垮裤 破球鞋 装扮的Devil
  3. 第二种装扮:
  4. 西装 领带 皮鞋 装扮的Devil

存在问题

现在如果要加超人装扮,只要增加子类就可以了,但是这么做虽然把“服装”类和“人”类分离开了,仍然是存在问题的。把“大T恤”、“垮裤”、“破球鞋”和“装扮的Devil”一个词一个词显示出来,就好比:你光着身子,当着大家的面,先穿T恤,再穿裤子,再穿鞋,仿佛在跳穿衣舞。。。因此需要一个房间(组合类)来换衣服,同时这个穿的顺序对每个人来说是不固定的,有的人喜欢先穿裤子,再穿鞋,最后穿T恤。。只需要把所需的功能按正确的顺序串联起来进行控制即可。

装饰模式

定义

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

UML图

  • Component是定义一个对象接口,可以给这些对象动态地添加职责。
  • ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。
  • Decorator,装饰抽象类,继承了Component,从外类来扩展Component类功能,但对于Component来说,是无需知道Decorator的存在的。
  • 至于ConcreteDecorator就是具体的装饰对象,起到了给Component添加职责的功能。

简单实现究极进化->装饰模式实现

代码

  1. /**
  2. * 究极进化人类(ConcreteComponent)
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class Person {
  6. private String name;
  7. public Person(){}
  8. public Person(String name){
  9. this.name = name;
  10. }
  11. public void show(){
  12. System.out.println("装扮的" + name);
  13. }
  14. }
  1. /**
  2. * 究极进化 服饰类(Decorator)
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public abstract class Finery extends Person{
  6. protected Person component;
  7. /**
  8. * 装扮
  9. * @param component
  10. */
  11. public void decorate(Person component) {
  12. this.component = component;
  13. }
  14. @Override
  15. public void show() {
  16. if (component != null) {
  17. component.show();
  18. }
  19. }
  20. }
  1. /**
  2. * 究极进化 T恤(ConcreteDecorator)
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class TShirts extends Finery{
  6. @Override
  7. public void show() {
  8. System.out.print("大T恤 ");
  9. super.show();
  10. }
  11. }
  1. /**
  2. * 究极进化 垮裤(ConcreteDecorator)
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class BigTrouser extends Finery {
  6. @Override
  7. public void show() {
  8. System.out.print("垮裤 ");
  9. super.show();
  10. }
  11. }
  12. // 其余子类相似,此处省略
  1. /**
  2. * 装饰模式测试
  3. * Created by callmeDevil on 2019/6/23.
  4. */
  5. public class Test {
  6. public static void main(String[] args) {
  7. Person devil = new Person("Devil");
  8. System.out.println("第一种装扮:");
  9. Sneakers sneakers = new Sneakers();
  10. BigTrouser bigTrouser = new BigTrouser();
  11. TShirts tShirts = new TShirts();
  12. // 装饰
  13. sneakers.decorate(devil);
  14. bigTrouser.decorate(sneakers);
  15. tShirts.decorate(bigTrouser);
  16. tShirts.show();
  17. System.out.println("\n第二种装扮:");
  18. LeatherShoes leatherShoes = new LeatherShoes();
  19. Tie tie = new Tie();
  20. Suit suit = new Suit();
  21. // 装饰
  22. leatherShoes.decorate(devil);
  23. tie.decorate(leatherShoes);
  24. suit.decorate(tie);
  25. suit.show();
  26. }
  27. }

测试结果

  1. 第一种装扮:
  2. T 垮裤 破球鞋 装扮的Devil
  3. 第二种装扮:
  4. 西装 领带 皮鞋 装扮的Devil

总结

  • 装饰模式是利用setComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。
  • 如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把DecoratorConcreteDecorator的责任合并成一个类。
  • 装饰模式是为已有功能动态地添加更多功能的一种方式
  • 不使用装饰模式,且系统需要新功能的时候,是向旧的类中添加新的代码,这些新加的代码通常装饰了原有类的核心职责或主要行为。在主类中加入了新的字段、方法和逻辑,会增加主类的复杂度,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊需求。装饰模式提供了非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象。
  • 装饰模式的优点是把类中装饰功能从类中搬移去除,这样可以简化原有的类。这么做最大的好处就是有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑。

原文链接:http://www.cnblogs.com/call-me-devil/p/11073500.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号