zoukankan      html  css  js  c++  java
  • [C# 设计模式] Adapter

    Adapter - 适配器模式

      现实生活中,我们常用到适配器。

      你当前打开我这篇文章的笔记本电脑,电源的另一边不正连着一块适配器吗?

      你平时想将三口插座插进二口插座里面,不也需要一个适配器吗?

      整天插在插座上的手机充电头,不也是一个适配器吗?

    目录

    • 第一种:类适配器(使用继承)
    • 第二种:对象适配器(使用委托)
    • 抽象的 UML 类图

    第一种:类适配器(使用继承)

      这里,我假设家用功率为 220v,经过适配器,输出为 18v,可供我的笔记本进行使用。

      类图

      Portal(入口) 类:只有一个方法 Input(),来表示输入的电流功率。

      IOutput(输出)接口:只有一个方法 Output(),来表示经过转换后输出的电流功率。

      Adapter(适配器)类:实现了 IOutput 接口。

      Portal.cs 类

     1     /// <summary>
     2     /// 入口
     3     /// </summary>
     4     class Portal
     5     {
     6         private readonly string _msg;
     7 
     8         public Portal(string msg)
     9         {
    10             _msg = msg;
    11         }
    12         
    13         /// <summary>
    14         /// 输入(电流)
    15         /// </summary>
    16         public void Input()
    17         {
    18             Console.WriteLine(_msg + " --> 18v。");
    19         }
    20     }

      

      IOutput.cs 接口

    1     interface IOutput
    2     {
    3         /// <summary>
    4         /// 输出(电流)
    5         /// </summary>
    6         void Output();
    7     }

      Adapter.cs 类

     1     /// <summary>
     2     /// 适配器
     3     /// </summary>
     4     class Adapter : Portal, IOutput
     5     {
     6         public Adapter(string msg) : base(msg)
     7         {
     8         }
     9 
    10         public void Output()
    11         {
    12             Input();
    13         }
    14     }

      Client.cs 类

     1     class Client
     2     {
     3         static void Main(string[] args)
     4         {
     5             IOutput adapter = new Adapter("220v");
     6             adapter.Output();
     7 
     8             Console.Read();
     9         }
    10     }

      客户端在使用的过程中,我们只知道输出的结果即可,内部实现不需要理会。

    第二种:对象适配器(使用委托)

      委托:自己不想做的事,交给第三方去做。

      类图

      Portal.cs 类

     1     /// <summary>
     2     /// 入口
     3     /// </summary>
     4     class Portal
     5     {
     6         private readonly string _msg;
     7 
     8         public Portal(string msg)
     9         {
    10             _msg = msg;
    11         }
    12 
    13         public void Input()
    14         {
    15             Console.WriteLine(_msg + " --> 18v");
    16         }
    17     }

      Adapter.cs 类

     1     class Adapter : Export
     2     {
     3         private readonly Portal _portal;
     4 
     5         public Adapter(string msg)
     6         {
     7             _portal = new Portal(msg);
     8         }
     9 
    10         public override void Output()
    11         {
    12             _portal.Input();
    13         }
    14     }

      Export.cs 类

    1     /// <summary>
    2     /// 出口
    3     /// </summary>
    4     abstract class Export
    5     {
    6         public abstract void Output();
    7     }

    抽象的 UML 类图

      4 种角色:Adaptee(被适配),Adapter(适配者),Client(使用场景),Target(目标对象)。

      Adaptee(被适配):不是 -er 结尾的哦,之前的 Portal(入口)类作为被适配者。

      Adapter(适配者):作为 Adaptee 和 Target 的媒介,进行调节。

      Client(使用场景):一个调用的入口,以 Main() 作为入口函数。

      Target(目标对象):调节(适配)后的输出,之前的 IOutput 接口和 Export 类都是作为 Target 对象。

     

    图:类适配器(使用继承)

    图:对象适配器(使用委托)

     C# 设计模式系列

      《Iterator - 迭代器模式:我与一份奥利奥早餐的故事


    【博主】反骨仔

    【原文】http://www.cnblogs.com/liqingwen/p/6560899.html 

     

  • 相关阅读:
    react router实现多级嵌套路由默认跳转
    【转载】git 撤销,放弃本地修改
    js中RGB值与16进制颜色值进行互转
    【转载】whistle 使用实践
    程序员腰突经历分享(中)
    在非洲运营互联网系统-如何搞定支付?
    30岁后遇不治之症(上)
    递归把path字符串构造成递归数组
    使用go开发公众号之 关注公众号发送小程序卡片
    excel 函数经验答题
  • 原文地址:https://www.cnblogs.com/liqingwen/p/6560899.html
Copyright © 2011-2022 走看看