[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 

 

posted @ 2017-03-18 10:56  反骨仔  阅读(2775)  评论(0编辑  收藏  举报