views:

158

answers:

2

I am trying to understand Adapter patter 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 sample 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 decison to use OracaleDAO was taken, so the code change

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

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();
tvanfosson
In your example, you have wrapped your framework class with a class that implements an Interface. I have understood this, but can you clarify how you have "transformed one interface to another? thanks.
pradeeptp
In this case, the interface technically didn't exist before. It's sort of a degenerate case in that you're actually just creating an interface for a class that didn't have one. The second example (just added) demonstrates this better.
tvanfosson
Thanks. This is much clear now!
pradeeptp
A: 

Here is a nice article that explains Adapter Pattern and its different types: http://developergeeks.com/article/39/introduction-to-adapter-design-pattern