手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#设计模式编程中运用适配器模式结构实战演练
C#设计模式编程中运用适配器模式结构实战演练
摘要:在实际的软件系统设计和开发中,为了完成某项工作需要购买一个第三方的库来加快开发。这带来一个问题,在应用程序中已经设计好的功能接口,与这个第三...

在实际的软件系统设计和开发中,为了完成某项工作需要购买一个第三方的库来加快开发。这带来一个问题,在应用程序中已经设计好的功能接口,与这个第三方提供的接口不一致。为了使得这些接口不兼容的类可以在一起工作,适配器模式提供了一种接口的适配机制。

适配器模式的设计思想在生活中经常会应用到,如我们在给手机充电的时候,不可能直接在220V电源上直接充电,而是用手机充电器转换成手机需要的电压才可以正常充电,否则就不可以完成充电,这个充电器就起到了适配的作用。

适配器模式结构实现

1.类适配器结构实现

C#设计模式编程中运用适配器模式结构实战演练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() 请按任意键继续. . .

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(); } } }

适配器模式实践应用

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

C#设计模式编程中运用适配器模式结构实战演练2

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的电压 得到手机的充电电压! 请按任意键继续. . .

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(); } } }

适配器模式的优缺点

在引言部分已经提出,适配器模式用来解决现有对象与客户端期待接口不一致的问题,下面详细总结下适配器两种形式的优缺点。

1.类的适配器模式:

优点:

可以在不修改原有代码的基础上来复用现有类,很好地符合 “开闭原则”

可以重新定义Adaptee(被适配的类)的部分行为,因为在类适配器模式中,Adapter是Adaptee的子类

仅仅引入一个对象,并不需要额外的字段来引用Adaptee实例(这个即是优点也是缺点)。

缺点:

用一个具体的Adapter类对Adaptee和Target进行匹配,当如果想要匹配一个类以及所有它的子类时,类的适配器模式就不能胜任了。因为类的适配器模式中没有引入Adaptee的实例,光调用this.SpecificRequest方法并不能去调用它对应子类的SpecificRequest方法。

采用了 “多继承”的实现方式,带来了不良的高耦合。

2.对象的适配器模式

优点:

可以在不修改原有代码的基础上来复用现有类,很好地符合 “开闭原则”(这点是两种实现方式都具有的)

采用 “对象组合”的方式,更符合松耦合。

缺点:

使得重定义Adaptee的行为较困难,这就需要生成Adaptee的子类并且使得Adapter引用这个子类而不是引用Adaptee本身。

使用场景

在以下情况下可以考虑使用适配器模式:

系统需要复用现有类,而该类的接口不符合系统的需求

想要建立一个可重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。

对于对象适配器模式,在设计里需要改变多个已有子类的接口,如果使用类的适配器模式,就要针对每一个子类做一个适配器,而这不太实际。

.NET中适配器模式的实现

1.适配器模式在.NET Framework中的一个最大的应用就是COM Interop。COM Interop就好像是COM和.NET之间的一座桥梁(关于COM互操作更多内容可以参考我的互操作系列)。COM组件对象与.NET类对象是完全不同的,但为了使.NET程序像使用.NET对象一样使用COM组件,微软在处理方式上采用了Adapter模式,对COM对象进行包装,这个包装类就是RCW(Runtime Callable Wrapper)。RCW实际上是runtime生成的一个.NET类,它包装了COM组件的方法,并内部实现对COM组件的调用。如下图所示:

C#设计模式编程中运用适配器模式结构实战演练3

2..NET中的另外一个适配器模式的应用就是DataAdapter。ADO.NET为统一的数据访问提供了多个接口和基类,其中最重要的接口之一是IdataAdapter。DataAdpter起到了数据库到DataSet桥接器的作用,使应用程序的数据操作统一到DataSet上,而与具体的数据库类型无关。甚至可以针对特殊的数据源编制自己的DataAdpter,从而使我们的应用程序与这些特殊的数据源相兼容。

【C#设计模式编程中运用适配器模式结构实战演练】相关文章:

c# 适配器模式

C#中常使用进度条的代码

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

C#中调用命令行cmd开启wifi热点的实例代码

c#设计模式 适配器模式详细介绍

C#访问应用程序配置文件的方法

C#获取进程的主窗口句柄的实现方法

C#动态执行字符串(动态创建代码)的实例代码

混合语言编程—C#使用原生的Directx和OpenGL绘图的方法

C#实现大数字运算的实例代码

精品推荐
分类导航