设计模式之禅之结构类PK【装饰模式VS适配器模式】

设计模式之禅PK之结构类

结构类设计模式

  • 结构类模式:
    • 适配器模式
    • 桥梁模式
    • 组合模式
    • 装饰模式
    • 门面模式
    • 享元模式
    • 代理模式
  • 相同点:他们都是通过组合类或对象产生更大的结构以适应更高的层次的逻辑需求

【装饰模式】VS【适配器模式】

  • 装饰模式和适配器模式在通用类图上没有太多的相似之处,差别比较大
  • 他们的功能上有很多【相似】的地方:
    • 都是包装作用
    • 都是通过委托方式实现其功能
  • 他们的【不同】点是:
    • 装饰包装的是自己的兄弟类,隶属于同一个家族
    • 适配器模式则修饰非血缘关系类,把一个非本家族的对象伪装成本家族的对象注意是伪装。因此本质上它还是非相同接口的对象
  • 从丑小鸭的华丽蜕变来开始
    • "话说鸭妈妈有5个孩子,其中4个孩子都是黄白相间的颜色,而最小的那只也就是叫做丑小鸭的那只,是纯白色的,与兄弟姐妹都不相同,在遭受了诸多的嘲讽和讥笑后,最终丑小鸭变成了一只美丽的天鹅"
    • 现在开始,进入丑小鸭的华丽蜕变过程吧!
装饰模式版的丑小鸭适配器模式版的丑小鸭
首先肯定丑小鸭就是一只天鹅,只是因为它从小或者是鸭妈妈的无知才没有被认出来是白天鹅,经过一段时间后,它逐步变成了一个漂亮、自信、优美的白天鹅需要从鸭妈妈的角度分析,四个鸭子都是真正的鸭子,丑小鸭实际不是鸭--“雄兔脚扑朔,雌兔眼迷离,双兔傍地走,安能辨我是雌雄”
设计:先设计一个丑小鸭,然后根据时间的先后来进行不同的美化处理--长出漂亮的羽毛--然后学会飞行--最后变成一只白天鹅设计:鸭和天鹅,然后鸭妈妈把一只天鹅看成了小鸭子,最终时间到来的时候丑小鸭变成了白天鹅

类图的比较

装饰模式适配器模式

代码详解

  • 装饰模式的丑小鸭的华丽蜕变

    • Swan

      package com.peng.pk_zs;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public interface Swan {
          // 天鹅会飞
          public void fly();
      
          // 天鹅会叫
          public void cry();
      
          // 天鹅都有漂亮的外表
          public void desAppaearance();
      }
      
    • Uglyducking

      package com.peng.pk_zs;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class Uglyducking implements Swan {
      
          @Override
          public void fly() {
              System.out.println("丑小鸭还比较小,还不能飞行!");
          }
      
          @Override
          public void cry() {
              System.out.println("叫声是克鲁-克鲁-克鲁。。");
          }
      
          @Override
          public void desAppaearance() {
              System.out.println("外形是脏兮兮的白色。毛茸茸的大脑袋!");
          }
      
      }
      
    • Decorator

      package com.peng.pk_zs;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class Decorator implements Swan {
          private Swan swan;
      
          public Decorator(Swan swan) {
              super();
              this.swan = swan;
          }
      
          @Override
          public void fly() {
              swan.fly();
          }
      
          @Override
          public void cry() {
              swan.cry();
          }
      
          @Override
          public void desAppaearance() {
              swan.desAppaearance();
          }
      
      }
      
    • StrongBehavior

      package com.peng.pk_zs;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class StrongBehavior extends Decorator {
          // 强化谁
          public StrongBehavior(Swan swan) {
              super(swan);
          }
      
          // 会飞行了
          @Override
          public void fly() {
              System.out.println("会飞行了!!!");
          }
      
      }
      
    • BeautifyAppearance

      package com.peng.pk_zs;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class BeautifyAppearance extends Decorator {
          // 要美化谁
          public BeautifyAppearance(Swan swan) {
              super(swan);
          }
      
          // 外表美化处理
          @Override
          public void desAppaearance() {
              System.out.println("外表是纯色到的,非常惹人喜爱!");
          }
      
      }
      
    • Client

      package com.peng.pk_zs;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class Client {
          public static void main(String[] args) {
              System.out.println("=====很久很久以前,这里有一只丑陋的小鸭子=====");
              Swan ducking = new Uglyducking();
              // 展示一下小鸭子
              ducking.desAppaearance();
              ducking.cry();
              ducking.fly();
      
              // 丑小鸭终于发现自己是一只白天鹅
              System.out.println("=====时间过去了很久,丑小鸭长大了=====");
              ducking = new BeautifyAppearance(ducking);
              ducking.desAppaearance();
              ducking = new StrongBehavior(ducking);
              ducking.fly();
          }
      }
      
    • 执行结果

      =====很久很久以前,这里有一只丑陋的小鸭子=====
      外形是脏兮兮的白色。毛茸茸的大脑袋!
      叫声是克鲁-克鲁-克鲁。。
      丑小鸭还比较小,还不能飞行!
      =====时间过去了很久,丑小鸭长大了=====
      外表是纯色到的,非常惹人喜爱!
      会飞行了!!!
      
  • 适配器模式的丑小鸭的华丽蜕变

    • Duck

      package com.peng.pk_sp;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public interface Duck {
          // 会叫
          public void cry();
      
          // 鸭子的外形
          public void desAppearance();
      
          // 鸭子的其他行为
          public void desBehavior();
      }
      
    • Ducking

      package com.peng.pk_sp;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class Ducking implements Duck {
      
          @Override
          public void cry() {
              System.out.println("叫声是:嘎嘎~~");
          }
      
          @Override
          public void desAppearance() {
              System.out.println("外形是黄白相间的,嘴长!");
          }
      
          @Override
          public void desBehavior() {
          System.out.println("会游泳~");
          }
      
      }
      
    • Swan

      package com.peng.pk_sp;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public interface Swan {
          // 天鹅会飞
          public void fly();
      
          // 天鹅会叫
          public void cry();
      
          // 天鹅都有漂亮的外表
          public void desAppaearance();
      }
      
    • WhiteSwan

      package com.peng.pk_sp;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class WhiteSwan implements Swan {
      
          @Override
          public void fly() {
              System.out.println("会飞~~");
          }
      
          @Override
          public void cry() {
              System.out.println("叫声:克鲁-克鲁");
          }
      
          @Override
          public void desAppaearance() {
              System.out.println("外形是纯白色,惹人喜爱!");
          }
      
      }
      
    • UglyDucking

      package com.peng.pk_sp;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class UglyDucking extends WhiteSwan implements Duck {
      
          @Override
          public void cry() {
              super.cry();
          }
      
          @Override
          public void desAppearance() {
              super.desAppaearance();
          }
      
          @Override
          public void desBehavior() {
              System.out.println("会游泳!");
              super.fly();
          }
      
      }
      
    • Client

      package com.peng.pk_sp;
      
      /**
       * @author kungfu~peng
       * @data 2017年12月13日
       * @description
       */
      public class Client {
          public static void main(String[] args) {
              // 鸭子
              System.out.println("鸭妈妈的四个孩子是这样的:");
              Duck duck = new Ducking();
              duck.cry();
              duck.desAppearance();
              duck.desBehavior();
              // 独特的丑小鸭
              System.out.println("丑小鸭长大后:");
              Duck dk = new UglyDucking();
              dk.cry();
              dk.desAppearance();
              dk.desBehavior();
          }
      }
      
    • 执行结果

      鸭妈妈的四个孩子是这样的:
      叫声是:嘎嘎~~
      外形是黄白相间的,嘴长!
      会游泳~
      丑小鸭长大后:
      叫声:克鲁-克鲁
      外形是纯白色,惹人喜爱!
      会游泳!
      会飞~~
      

最佳实践

  • 装饰模式与适配器模式的不同
不同装饰模式适配器模式
意图加强对象的功能(也可以削弱)转化(把一个天鹅当做一个鸭子来看待)
施与对象自己的同宗两个不同的对象
场景不同任何时候补救模式(项目已经完毕:紧急处理手段)
扩展性不同容易

声明

  • 摘自秦小波《设计模式之禅》第2版;
  • 仅供学习,严禁商业用途;
  • 代码手写,没有经编译器编译,有个别错误,自行根据上下文改正;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值