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(); } } }
适配器模式实践应用 以手机充电的电源适配器为例,用适配器模式的解决方案。 1.类适配器结构实现 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.对象适配器结构实现 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(); } } } 适配器模式的优缺点 使用场景 .NET中适配器模式的实现 2..NET中的另外一个适配器模式的应用就是DataAdapter。ADO.NET为统一的数据访问提供了多个接口和基类,其中最重要的接口之一是IdataAdapter。DataAdpter起到了数据库到DataSet桥接器的作用,使应用程序的数据操作统一到DataSet上,而与具体的数据库类型无关。甚至可以针对特殊的数据源编制自己的DataAdpter,从而使我们的应用程序与这些特殊的数据源相兼容。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |