欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-09-08 10:27:09
 在实际的软件系统设计和开发中,为了完成某项工作需要购买一个第三方的库来加快开发。这带来一个问题,在应用程序中已经设计好的功能接口,与这个第三方提供的接口不一致。为了使得这...

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


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

适配器模式结构实现

1.类适配器结构实现

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

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#设计模式编程中运用适配器模式结构实战演练

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#设计模式编程中运用适配器模式结构实战演练

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