1. 适配器模式简介

1.1 模式定义

  适配器模式:通过一个类的接口转换成客户希望的另外一个接口,使原本由于接口不兼容而不能一起工作的那些类可以一起工作

  适配器从结构上分为:类适配器和对象适配器。其中类适配器使用继承关系来对类进行适配,对象适配器使用对象引用来进行适配。

  C#实现类适配器时,Target只能是接口。实现对象适配器时,Target可以是抽象类也可以是接口。

1.2 使用频率

   中高

2. 类适配器模式结构

2.1 结构图

2.1.1 类适配器结构图

2.1.2 对象适配器结构图

2.2 参与者

  适配器模式参与者:

  ◊ Target:Client所使用的目标接口,可以是接口或抽象类。由于C#不支持多类继承,故把Target定义为接口。

  ◊ Adaptee:需要适配的类接口。

  ◊ Adapter:适配器,负责Adaptee的接口与Target接口进行适配。

  ◊ Client:与符合Target接口的对象协调的类。

  在适配器模式中,类Adapter实现适配器的功能,它在Client与Adaptee之间加入Adapter,这样Client把请求发给接口为Target的类Adapter,再由Adapter调用Adaptee,从而实现Client调用Adaptee。

3. 适配器模式结构实现

3.1 类适配器结构实现

  ITarget.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
public interface ITarget
{
void Request();
}
}

  Adaptee.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
public class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()");
}
}
}

  Adapter.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
public class Adapter : Adaptee, ITarget
{
public void Request()
{
this.SpecificRequest();
}
}
}

  Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
public class Client
{
static void Main(string[] args)
{
ITarget t = new Adapter();
t.Request();
}
}
}

  运行输出:

Called SpecificRequest()
请按任意键继续. . .

3.2 对象适配器结构实现

  Client需要调用Request方法,而Adaptee并没有该方法,为了使Client能够使用Adaptee类,需要提供一个类Adapter。这个类包含了一个Adaptee的实例,将Client与Adaptee衔接起来。

  ITarget.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
public interface ITarget
{
void Request();
}
}

  Target.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
public class Target : ITarget
{
public virtual void Request()
{
Console.WriteLine("Called Target Request()");
}
}
}

  Adaptee.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
public class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()");
}
}
}

  Adapter.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
public class Adapter : Target
{
private Adaptee _adaptee = new Adaptee(); public override void Request()
{
_adaptee.SpecificRequest();
}
}
}

  Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
public class Client
{
static void Main(string[] args)
{
ITarget t = new Adapter();
t.Request();
}
}
}

4. 适配器模式实践应用

  以手机充电的电源适配器为例,用适配器模式的解决方案。

4.1 类适配器结构实现

  ITarget.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
public interface ITarget
{
void GetPower();
}
}

  Power.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
public class Power
{
public void GetPower220V()
{
Console.WriteLine("从电源中得到220V的电压");
}
}
}

  Adapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
public class Adapter : Power, ITarget
{
public void GetPower()
{
this.GetPower220V();
Console.WriteLine("得到手机的充电电压!");
}
}
}

  Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
public class Client
{
static void Main(string[] args)
{
Console.WriteLine("手机:");
ITarget t = new Adapter();
t.GetPower();
}
}
}

  运行输出:

手机:
从电源中得到220V的电压
得到手机的充电电压!
请按任意键继续. . .

4.2>、对象适配器结构实现

  ITarget.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
public interface ITarget
{
void GetPower();
}
}

  Power.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
public class Power
{
public void GetPower220V()
{
Console.WriteLine("从电源中得到220V的电压");
}
}
}

  Adapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
public class Adapter : ITarget
{
public Power _power;
public Adapter(Power power)
{
this._power = power;
} /// <summary>
/// 得到想要的电压
/// </summary>
public void GetPower()
{
_power.GetPower220V();
Console.WriteLine("得到手机的充电电压!");
}
}
}

  Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
public class Client
{
static void Main(string[] args)
{
Console.WriteLine("手机:");
ITarget t = new Adapter(new Power());
t.GetPower();
}
}
}

5、适配器模式应用分析

  适配器模式适用情形:

  ◊ 当适用一个已存在的类,而它的接口不符合所要求的情况;

  ◊ 想要创建一个可以复用的类,该类可以与原接口的类协调工作;

  ◊ 在对象适配中,当要匹配数个子类的时候,对象适配器可以适配它们的父类接口。

   适配器模式特点:

  类适配器

  ◊ 使得Adapter可以重定义Adaptee的部分行为。因为Adapter是Adaptee的一个子类;

  ◊ 仅仅引入了一个对象,并不需要额外的指针间接得到Adaptee。

  对象适配器

  ◊ 允许一个Adapter与多个Adaptee同时工作。Adapter也可以一次给所有的Adaptee添加功能;

  ◊ 使得重定义Adaptee的行为比较困难。需要生成一个Adaptee的子类,然后使Adapter引入这个子类而不是引用Adaptee本身。

最新文章

  1. Android入门(一)
  2. 整理PHP_YII环境安装遇到的一些问题
  3. [原] Android performClick无效,UI线程理解
  4. python知识总结
  5. 一个关于C#中基类与接口混合继承的疑问总结
  6. hdu 3886 Final Kichiku “Lanlanshu” 数位DP
  7. 西南科技大学第十一届ACM程序设计大赛发言稿
  8. 三维FEM的刚度矩阵数量级
  9. Textarea - 百度富文本编辑器插件UEditor
  10. css样式实现字体删除线效果
  11. CompletionService 简介
  12. BZOJ1187 [HNOI2007]神奇游乐园(插头dp)
  13. MySql数据库时区异常,java.sql.SQLException: The server time zone value &#39;?&#208;&#185;???&#215;&#188;&#202;&#177;?&#39; is unrecognized or represents more than one time zone.
  14. No cached version of cn.lightsky.infiniteindicator:library:1.2.2 available for offline mode.
  15. [LeetCode&amp;Python] Problem 1: Two Sum
  16. 转的很好的前端html 内容
  17. IntelliJ IDEA的黑白色背景切换(Ultimate和Community版本皆通用)
  18. Python中super的应用
  19. [Selenium]通过JavaScript来对隐藏的元素执行操作
  20. vmvare fusion 8

热门文章

  1. 很好的一篇eureka的讲解文章
  2. Java基础之多线程篇(线程创建与终止、互斥、通信、本地变量)
  3. 【C语言】结构体占用字节数及存储与空间分配
  4. kubernetes 里面pod时间修改
  5. Luogu3191 HNOI2007 紧急疏散 二分答案、最大流
  6. [转]The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path
  7. [Spark][Python]获得 key,value形式的 RDD
  8. Scala学习(四)练习
  9. 《Head First 设计模式》例子的C++实现(2 观察者模式)
  10. AT2134 Zigzag MST