适配

即在不改变原有实现的基础上,将原先不兼容的接口转换为兼容的接口。
例如:二转换为三箱插头,将高电压转换为低电压等。

动机(Motivate):
    在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象”放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足的。
    那么如何应对这种“迁移的变化”?如何既能利用现有对象的良好实现,同时又能满足新的应用环境所要求的接口?这就是本文要说的Adapter 模式。
意图(Intent):
    将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
                                                                            -------《设计模式》GOF

抽象的 UML 类图

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

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

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

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

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

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

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

     /// <summary>
/// 数据访问接口
/// </summary>
public interface IHelper
{
void Add<T>();
void Delete<T>();
void Update<T>();
void Query<T>();
}
     public class MysqlHelper : IHelper
{
public void Add<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void Delete<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void Update<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void Query<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}
     public class OracleHelper : IHelper
{
public void Add<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void Delete<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void Update<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void Query<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}
     public class SqlserverHelper : IHelper
{
public void Add<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void Delete<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void Update<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void Query<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}

上面的代码mysql Oracle SQLserver都实现了IHelper接口这里如果想要对接Redis就需要使用适配器

     /// <summary>
/// 第三方提供的 openstack servicestack
/// 不能修改
/// </summary>
public class RedisHelper
{
public void AddRedis<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void DeleteRedis<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void UpdateRedis<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void QueryRedis<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}
     /// <summary>
/// 通过继承 类适配器模式
/// </summary>
public class RedisHelperClass : RedisHelper, IHelper
{
public void Add<T>()
{
base.AddRedis<T>();
} public void Delete<T>()
{
base.DeleteRedis<T>();
} public void Update<T>()
{
base.UpdateRedis<T>();
} public void Query<T>()
{
base.QueryRedis<T>();
}
}

另外还可以使用适配器执行方法前后做一些拓展

     public class CacheHelper
{
public void AddCache<T>()
{
Console.WriteLine("This is {0} Add", this.GetType().Name);
}
public void DeleteCache<T>()
{
Console.WriteLine("This is {0} Delete", this.GetType().Name);
}
public void UpdateCache<T>()
{
Console.WriteLine("This is {0} Update", this.GetType().Name);
}
public void QueryCache<T>()
{
Console.WriteLine("This is {0} Query", this.GetType().Name);
}
}
     /// <summary>
/// 通过组合 对象适配器模式
///
/// 组合优于继承
/// </summary>
public class RedisHelperObject : IHelper
{
//private RedisHelper _RedisHelper = new RedisHelper();
private RedisHelper _RedisHelper = null; private CacheHelper _CacheHelper = new CacheHelper();
public RedisHelperObject(RedisHelper redisHelper)//可能是一个抽象接口,注入进来
{
this._RedisHelper = redisHelper;
} public RedisHelperObject()
{
this._RedisHelper = new RedisHelper();
} public void Add<T>()
{
this._CacheHelper.AddCache<T>();
this._RedisHelper.AddRedis<T>();
} public void Delete<T>()
{
this._RedisHelper.DeleteRedis<T>();
} public void Update<T>()
{
this._RedisHelper.UpdateRedis<T>();
} public void Query<T>()
{
this._RedisHelper.QueryRedis<T>();
}
}

前端调用

                 Console.WriteLine("*****************************");
{
IHelper helper = new SqlserverHelper();
helper.Add<Program>();
helper.Delete<Program>();
helper.Update<Program>();
helper.Query<Program>();
}
Console.WriteLine("*****************************");
{
IHelper helper = new MysqlHelper();
helper.Add<Program>();
helper.Delete<Program>();
helper.Update<Program>();
helper.Query<Program>();
}
Console.WriteLine("*****************************");
{
IHelper helper = new OracleHelper();
helper.Add<Program>();
helper.Delete<Program>();
helper.Update<Program>();
helper.Query<Program>();
}
Console.WriteLine("*****************************");
{
IHelper helper = new RedisHelperClass(); //new RedisHelper();
helper.Add<Program>();
helper.Delete<Program>();
helper.Update<Program>();
helper.Query<Program>();
}
{
//侵入性特别强
RedisHelperClass helper = new RedisHelperClass();
helper.Add<Program>();
//helper
}
Console.WriteLine("*****************************");
{
IHelper helper = new RedisHelperObject(); //new RedisHelper();
helper.Add<Program>();
helper.Delete<Program>();
helper.Update<Program>();
helper.Query<Program>();
}

Adapter模式的几个要点:
    Adapter模式主要应用于“希望复用一些现存的类,但是接口又与复用环境要求不一致的情况”,在遗留代码复用、类库迁移等方面非常有用。
    GOF23定义了两种Adapter模式的实现结构:对象适配器和类适配器。但类适配器采用“多继承”的实现方式,带来不良的高耦合,所以一般不推荐使用。对象适配器采用“对象组合”的方式,更符合松耦合精神。
    Adapter模式可以实现的非常灵活,不必拘泥于GOF23中定义的两种结构。例如,完全可以将Adapter模式中的“现存对象“作为新的接口方法参数,来达到适配的目的。
    Adapter模式本身要求我们尽可能地使用”面向接口的编程"风格,这样才能在后期很方便的适配。
.NET框架中的Adapter应用:
(1)在.Net中复用com对象:
 Com 对象不符合.net对象的接口
使用tlbimp.exe来创建一个Runtime Callable Wrapper(RCW)以使其符合.net对象的接口。
(2).NET数据访问类(Adapter变体):
各种数据库并没有提供DataSet接口
使用DBDataAdapter可以将任何各数据库访问/存取适配到一个DataSet对象上。
(3)集合类中对现有对象的排序(Adapter变体);
现有对象未实现IComparable接口
实现一个排序适配器(继承IComparer接口),然后在其Compare方法中对两个对象进行比较。

本文参考文档:https://www.cnblogs.com/abcdwxc/archive/2007/09/04/881674.html

https://www.cnblogs.com/liqingwen/p/6560899.html

23种设计模式之适配器模式(Adapter Pattern)的更多相关文章

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

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

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

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

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

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

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

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

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

    Adapter Pattern的作用是在不改变功能的前提下转换接口.Adapter分为两类,一类是Object Adapter, 还有一类是Class Adapter.因为Class Adapter的 ...

  6. 【Unity与23种设计模式】适配器模式(Adapter)

    GoF中定义: "将一个类的接口转换成为客户端期待的类接口.适配器模式让原本接口不兼容的类能一起合作." 适配器模式与装饰模式有一定的相似之处 两者都是在着手解决C#不能多继承的问 ...

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

    适配器模式将一个接口转换成客户希望的另一个接口,从而使接口不兼容的那些类可以一起工作.适配器模式既可以作为类结构型模式,也可以作为对象结构型模式.在类适配器模式中,通过使用一个具体类将适配者适配到目标 ...

  8. 夜话JAVA设计模式之适配器模式(adapter pattern)

    适配器模式:将一个类的接口,转换成客户期望的另一个接口,让不兼容的接口变成兼容. 1.类适配器模式:通过多重继承来实现适配器功能.多重继承就是先继承要转换的实现类,再实现被转换的接口. 2.对象适配器 ...

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

    概述 描述 将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper). 套路 Target(目标抽象类) 目标抽象类定义了客户所需要的接口,可 ...

随机推荐

  1. 2016 ACM-ICPC 青岛站网络赛G题 题解

    [参考博客][https://blog.csdn.net/Tawn0000/article/details/82255682] 题意: 将n个数按照每k个一组来合并,合并需要花费的cost是两个数的长 ...

  2. Linux之Shell编程(14)

    变量: 定义变量的规则: 1)变量名可以由字母.数字和下划线组成,但不能以数字开头 2)等号两侧不能有空格 3)变量名一般习惯大写 将命令的返回值赋值给变量: 1)使用``将命令括起来 2)使用$() ...

  3. 那些让你觉得自己是个傻B的题目集锦(大神的降维打击合集)

    一起过来排好队,进来挨打 1.Leetcode tag-LinkList 109.convert sorted list to binary search tree 2Leetcode tag-Arr ...

  4. hdu-6638 Snowy Smile

    题目链接 Snowy Smile Problem Description There are n pirate chests buried in Byteland, labeled by 1,2,-, ...

  5. gym/101955/problem/E - The Kouga Ninja Scrolls 线段数 维护 切比雪夫距离 2018沈阳icpc

    传送门 思路: 这道题要把给定的每个坐标利用切比雪夫坐标表示,这样两个点的距离就是max(dx,dy),而不是一开始的dx + dy,有利于线段树的维护,又由于询问的是区间的最大差值,限制是两个点是属 ...

  6. HDU-2089不要62-暴力或数位DP入门

    不要62 题意:给定区间,求在这个区间中有多少个数字,不包含4且不包含62: 这道题作为数位DP的入门题: 暴力也是可以过 #include<cstdio> #include <io ...

  7. 线段树(求单结点) hdu 1556 Color the ball

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. Python起源发展与学(ru)习(keng)的开篇

    写在前面,python天下第一!!!!!我要做python的舔狗

  9. iOS组件化实践

    参考资料: http://wereadteam.github.io/2016/03/19/iOS-Component/#more https://casatwy.com/iOS-Modulizatio ...

  10. MVC设计模式用于用户注册表单提交到数据库的中文乱码问题

    本文引用自:http://blog.csdn.net/wangchangshuai0010/article/details/12714575 java.sql.SQLException: Incorr ...