了解适配器模式 [英] Understanding Adapter Pattern

查看:186
本文介绍了了解适配器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解适配器模式及其在现实世界中使用。通过互联网和www.dofactory.com各种物品去之后,我创造了这个示例代码。我只是想知道我的理解是否正确。在下面的例子中我在适配器类创建MSDAO对象。后来我改成了OracleDAO。



<预类=郎-CS prettyprint-覆盖> 类客户端
{
静无效的主要(字串[] args)
{
ITarget objAdapter =新的适配器();
对象dummyObject = objAdapter.GetData();
}
}

接口ITarget
{
公共无效的GetData();
}

//决定使用MSDAO

类适配器:ITarget
{
公共无效的GetData()
{
MSDAO objmsdao =新MSDAO();
objmsdao.GetData();
}
}

//一个月后,使用OracaleDAO作出上述决定的,所以代码更改

类适配器:ITarget
{
公共无效的GetData()
{
OracleDAO objoracledao =新OracleDAO();
objoracledao.GetData();
}
}


解决方案

一般适配器模式转变一个接口到另一个,但它可以简单地包裹的行为,你的类从底层实现隔离。在你的情况,你正在使用的适配器,但是你可以很容易地定义了DAO对象简单地实现该接口和对接口进行编程。适配器模式通常用于当你没有对目标类的控制。我的适配器模式的主要用途是来为没有实现接口的框架类包装。



说我想要模拟出一个框架类,它没有按'吨实现一个接口(并且不具有虚拟方法)。由于许多嘲讽的API,这是很难或不可能的事情。我将做什么的话,就是定义自己的界面,我针对的类的签名的子集。我实现了实现了这个接口,只是委托调用包装的框架类的包装类。此包装类的工作作为框架类的适配器。我的班级使用此适配器,而不是框架类,但得到的框架类的行为。

 公共接口IFoo的
{
无效酒吧();
}

公共类FooWrapper:IFoo的
{
私人FrameworkFoo美孚{搞定;组; }

公共FooWrapper(FrameworkFoo富)
{
this.Foo = foo的;
}

公共无效酒吧()
{
this.Foo.Bar();
}
}



想想也是,你有几个不同的情况下,具有基本相同的功能,但不同的签名,并且希望能够交替使用这些类。如果你不能把这些(或不想因为其他原因),你可能会想要写一个适配器类,定义一个通用的接口和目标类可用该接口的方法和方法之间转换。



框架类:

 公共类TargetA 
{
酒店的公共空间开始(){...}
公共无效结束(){...}
}

公共类TargetB
{
公共无效开始(){...}
公共无效终止(){...}
}

对于他们的适配器

 公共接口ITargetAdapter 
{
无效公开赛();
无效关闭();
}

公共类AdapterA:ITargetAdapter
{
私人TargetA A {搞定;组; }

公共AdapterA(TargetA一)
{
this.A =一;
}

公共无效的open(){this.A.Start(); }
公共无效关闭(){this.A.End(); }
}

公共类适配器B:ITargetAdapter
{
私人TargetB B {搞定;组; }

公共适配器B(TargetB一)
{
this.B =一;
}

公共无效的open(){this.B.Begin(); }
公共无效关闭(){this.B.Terminate(); }
}



然后将其用作:

  ITargetAdapter适配器=新AdapterA(新TargetA()); 
adapter.Open();
adapter.Close();


I am trying to understand Adapter pattern and its use in real world. After going through various articles on internet and www.dofactory.com, I created this sample code. I just want to know whether my understanding is correct. In the example below I have created MSDAO object in the Adaptor class. Later I changed it to OracleDAO.

class Client
{
  static void Main(string[] args)
  {
    ITarget objAdapter = new Adapter();
    object dummyObject = objAdapter.GetData();
  }
}

Interface ITarget
{
  public void GetData();
}

//Decision to use MSDAO

class Adapter : ITarget
{
  public void GetData()
  {
    MSDAO objmsdao = new MSDAO();
    objmsdao.GetData();
  }
}

//After a month, the decision to use OracaleDAO was taken, so the code change

class Adapter : ITarget
{
  public void GetData()
  {
    OracleDAO objoracledao = new OracleDAO();
    objoracledao.GetData();
  }
}

解决方案

Generally the adapter pattern transforms one interface into another, but it can simply wrap the behavior to isolate your class from the underlying implementation. In your case, you are using an adapter, but you could just as easily have defined the DAO objects to simply implement the interface and programmed against the interface. The adapter pattern is usually used when you don't have control over the target class. My primary use of the adapter pattern would be to create wrappers for a framework class that doesn't implement an interface.

Say I want to mock out a framework class which doesn't implement an interface (and doesn't have virtual methods). With many mocking apis this is hard or impossible to do. What I will do, then, is define my own interface as a subset of the signature of the class I'm targeting. I implement a wrapper class that implements this interface and simply delegates the calls to the wrapped framework class. This wrapper class works as an adapter for the framework class. My classes use this adapter instead of the framework class, but get the framework class' behavior.

 public interface IFoo
 {
     void Bar();
 }

 public class FooWrapper : IFoo
 {
      private FrameworkFoo Foo { get; set; }

      public FooWrapper( FrameworkFoo foo )
      {
           this.Foo = foo;
      }

      public void Bar()
      {
           this.Foo.Bar();
      }
 }

Consider also the case where you have a couple of different classes that have basically the same functionality, but different signatures and you want to be able to use them interchangeably. If you can't transform these (or don't want to for other reasons), you may want to write an adapter class that defines a common interface and translates between that interface's methods and the methods available on the target classes.

Framework classes:

public class TargetA
{
    public void Start() { ... }
    public void End() { ... }
}

public class TargetB
{
    public void Begin() { ... }
    public void Terminate() { ... }
}

An adapter for them

public interface ITargetAdapter
{
    void Open();
    void Close();
}

public class AdapterA : ITargetAdapter
{
     private TargetA A { get; set; }

     public AdapterA( TargetA a )
     {
           this.A = a;
     }

     public void Open() { this.A.Start(); }
     public void Close() { this.A.End(); }
}

public class AdapterB : ITargetAdapter
{
     private TargetB B { get; set; }

     public AdapterB( TargetB a )
     {
           this.B = a;
     }

     public void Open() { this.B.Begin(); }
     public void Close() { this.B.Terminate(); }
}

Then used as:

ITargetAdapter adapter = new AdapterA( new TargetA() );
adapter.Open();
adapter.Close();

这篇关于了解适配器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆