绝大多数公司都有自己的一个客户管理系统,CRM是公司内部经营的产业链。有时因为部门的不同,赋予每个部门同一个功能的命名也许不同,比如市场部在注册人数上称之为有效人数,而客服部管理中就是想称之为今日注册,当我们考虑代码复用,让class有机工作在一起的时候,就需要考虑Adapter pattern了。
概述:
在软件开发过程中,我们要经常要复用我们以前开发的一些“现存对象”,但是这些“现存对象”并不能满足我们新的应用环境。怎样才能良好的复用这些对象,以满足我们的应用环境,这就是适配器(Adapter)所要解决的问题。
意图:
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
当service class首先使用了类Service,而有一个叫做TodayRegister()方法,当我们想要在市场部添加一个查看有效人数的时候,而我们在设计中又将此Market class的接口称作为EffectiveRegister(),这个时候便可以考虑使用Adapter来实现代码的重用,用以调用客服部统一功能但不同接口名字的方法。代码如下:
1.Adapter Pattern of Class
using System; using System.Collections.Generic; using System.Text; namespace AdapterPattern { public interface IIterface { string EffectiveCount(); } class ServiceAdaptee { public string TodayRegister() {
//todo sth some departments needed by the number of registers this day return "number of register from service class"; } } class MarkeyAdapter : ServiceAdaptee, IIterface { public string EffectiveCount() {
//here,i will add sth to the value returned,such as return "here is market,the effective num is:"+this.TodayRegister(); } } class Program { static void Main(string[] args) { MarkeyAdapter aAdapter = new MarkeyAdapter(); Console.WriteLine(aAdapter.EffectiveCount()); } } }
2.Adapter Pattern of Object
using System; using System.Collections.Generic; using System.Text; namespace AdapterPattern { public interface IIterface { string EffectiveCount(); } class ServiceAdaptee { public string TodayRegister() { return "number of register from service class"; } } class MarkeyAdapter : IIterface { private ServiceAdaptee aServiceAdaptee; public MarkeyAdapter() { aServiceAdaptee = new ServiceAdaptee(); } public string EffectiveCount() { return aServiceAdaptee.TodayRegister(); } } class Program { static void Main(string[] args) { MarkeyAdapter aAdapter = new MarkeyAdapter(); Console.WriteLine(aAdapter.EffectiveCount()); } } }
适配器模式的用意是将接口不同而功能相同或者相近的两个接口加以转换,包括适配器角色补充一些源角色没有但目标接口需要的方法。但不要误以为适配器模式就是为了补充源角色没有的方法而准备的。
类适配器模式与对象适配器模式的区别:
1.使用对象适配器模式可以把多种不同的源适配到同一个目标。换言之,同一个适配器可以把源类和它的子类都适配到目标接口。
2.使用对象适配器模式增加的新方法可以同时适用所有的源。
(对对象的适配器模式而言)在设计里,需要改变多个已有的子类的接口,如果使用类的适配器模式,就要针对每一个子类做一个适配器类,而这不太实际。
与类的适配器模式相比,要想置换源类的方法不容易。 这需要在抽象内容中使用。
如果源中有大量的方法,使用类的适配器模式则比较容易(不需要一一写出源类中的方法,而对象的适配器模式则要一一写出目标角色中的方法,如缺省适配器)。
此类情况代码参考:

using System; using System.Collections.Generic; using System.Text; namespace AdapterPattern { public interface IIterface { string EffectiveCount(); } class ServiceAdaptee { public virtual string TodayRegister() { return "number of register from service class"; } } class ServiceAdapteeSon : ServiceAdaptee { public override string TodayRegister() { return "number of register from service son"; } } //class MarkeyAdapter : IIterface //{ // private ServiceAdaptee aServiceAdaptee; // public MarkeyAdapter(ServiceAdaptee a) // { // aServiceAdaptee = a; // } // public string EffectiveCount() // { // return aServiceAdaptee.TodayRegister(); // } //} class MarkeyAdapter : ServiceAdapteeSon, IIterface { public string EffectiveCount() { return this.TodayRegister(); } } class Program { static void Main(string[] args) { MarkeyAdapter aAdapter = new MarkeyAdapter(); //MarkeyAdapter aAdapter = new MarkeyAdapter(new ServiceAdapteeSon()); Console.WriteLine(aAdapter.EffectiveCount()); } } }
在什么情况下使用适配器模式:
1。系统需要使用现有的类,而此类的接口不符合系统的需要。
2。想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。这些源类不一定有很复杂的接口。
3。类或方法的作用相同但名称不同的类或方法之间进行适配(by kyo)。
与桥梁模式的区别:
桥梁模式的用意是要把实现和它的接口分开,以便它们可以独立地变化。桥梁模式并不是用来把一个已有的对象接到不相匹配的接口上的。当一个客户端只知道一个特定的接口,但是又必须与具有不同接口的类打交道时,就应该使用适配器模式。
缺省适配模式(Default Adapter Pattern)为一个接口提供缺省实现,这样子类型可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。
适配器模式的用意是要改变源的接口,以便与目标类的接口相容。缺省适配的用意稍有同,它是为了方便建立一个不平庸的适配器类而提供的一种平庸实现(就是为接口提供空的实现)。
在任何时候,如果不准备实现一个接口的所有方法时(一般在定义一个大的接口时都应该提供它的缺省适配类),就可以制造一个抽象类,给出所有方法的平庸的具体实现。这样,从这个抽象类再继承下去的子类就不必实现所有的方法了。缺省适配模式的中心是一个缺省适配类。这个类应当是抽象类。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述