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本身。

设计模式笔记:适配器模式(Adapter)的更多相关文章

  1. 设计模式 笔记 适配器模式 Adapter

    //---------------------------15/04/13---------------------------- //Adapter 适配器模式 ----类对象结构型模式 /* 1: ...

  2. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  3. 8.3 GOF设计模式二: 适配器模式 Adapter

    GOF设计模式二: 适配器模式 Adapter  为中国市场生产的电器,到了美国,需要有一个转接器才能使用墙上的插座,这个转接 器的功能.原理?复习单实例模式  SingleTon的三个关键点  ...

  4. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  5. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  6. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  7. Java设计模式之适配器模式(Adapter)

    转载:<JAVA与模式>之适配器模式 这个总结的挺好的,为了加深印象,我自己再尝试总结一下 1.定义: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法 ...

  8. JavaScript设计模式 Item9 --适配器模式Adapter

    适配器模式(转换器面模式),通常是为要使用的接口,不符本应用或本系统使用,而需引入的中间适配层类或对象的情况. 适配器模式的作用是解决两个软件实体间的接口不兼容的问题. 一.定义 适配器模式(Adap ...

  9. python 设计模式之适配器模式 Adapter Class/Object Pattern

    #写在前面 看完了<妙味>和<华医>,又情不自禁的找小说看,点开了推荐里面随机弹出的<暗恋.橘生淮南>,翻了下里面的评论,有个读者从里面摘了一段自己很喜欢的话出来, ...

  10. [设计模式] 7 适配器模式 adapter

    在 Adapter 模式的结构图中可以看到,类模式的 Adapter 采用继承的方式复用 Adaptee的接口,而在对象模式的 Adapter 中我们则采用组合的方式实现 Adaptee 的复用 类模 ...

随机推荐

  1. centos6安装tomcat8.5

    //参考https://www.cnblogs.com/xdp-gacl/p/4097608.html [root@192 ~]# mount /dev/sr0 /mnt/usb1[root@192 ...

  2. 小程序 获取微信小程序的源码

    1.微信小程序是以wxapkg可执行文件的形式存在本地的 2.网上有工具可以把wxapkg文件还原成源代码: https://github.com/qwerty472123/wxappUnpacker ...

  3. Git使用—第二讲

    前面我们学习了Git最基本的用法,包括安装Git.创建代码仓库,以及提交本地代码.下面我们将学习Git更多的使用技巧,在开始之前,我们先给一个项目创建代码仓库,这里选择在ProviderTest项目中 ...

  4. 浅谈UI自动化测试

    最近一直在学习python,正好部门技术结构调整,就开始了点工向UI自动化测试的转变,我要说瞌睡来了就掉枕头么? 不过还好,可以将python的学习成果在自动化测试中实践... 1.about自动化测 ...

  5. java.lang.UnsatisfiedLinkError: No implementation found for int com.baidu.platform.comjni.map.commonmemcache.JNICommonMemCache.Create()

    完整异常: Process: com.example.ai.tabhostdemo, PID: 1287 java.lang.UnsatisfiedLinkError: No implementati ...

  6. SkylineGlobe 移动端开发测试

    基于SkylineGlobe提供的安卓版本SDK,在已有菜单中增加自定义内容,测试代码如下: 新增加文件ZhaoHeContainer.java package com.skyline.terraex ...

  7. 5、数组&字符串&结构体&共用体&枚举

    程序中内存从哪里来 三种内存来源:栈(stack).堆(heap).数据区(.date): 栈(stack) 运行自动分配.自动回收,不需要程序员手工干预: 栈内存可以反复使用: 栈反复使用后,程序不 ...

  8. 广电的宽带网络真流氓,替换google的广告为百度的广告

    以前联通也有干过这事,最近联通,有没有继续干,不清楚.没有用联通了. 最近,连到某wifi,发现网站的google广告,居然显示成百度的,特别去访问另一家网站,发现,本该是google广告的位置,同样 ...

  9. python语言程序设计8

    1, 说实话,我挺伤心的,感觉  有点像烂剧里的主演...也许我早几天明白的话,会不会结果会不一样?但是之前还真没往这方面想过,但是确实是开了一个口子了,也不急吧.努力把现在的事给做好,变帅变高,那很 ...

  10. ABP从入门到精通(5):.扩展国际化语言资源

    ABP的有些组件使用的该组件自带的语言包资源,所以在有些时候会因为我们当前使用的语言对应的语言包不全,而造成日志一直记录WARN.ABP给我们提供了扩展语言包资源的接口,可以解决这个问题. 以下示例代 ...