SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Adapter pattern

rated by 0 users
This post has 2 Replies | 0 Followers

Top 25 Contributor
Posts 232
Thanh Posted: 08-21-2005 1:55 AM

Adpater pattern converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

What is the practical usage of this pattern? Many frameworks use this pattern to convert different interfaces written in different languages so that they can communicate seamlessly and transparently with each other

Client.cs
using System;
using BusinessLogic;

namespace Test
{
  public class Client
  {
    public static void Main(string[] args)
    {
      VoltageAdapter Adapter = new VoltageAdapter();
      Console.WriteLine(Adapter.Request(0));
      Console.WriteLine(Adapter.Request(1));
      Console.WriteLine(Adapter.Request(13));    
    }
  }   

BL.cs
using System;

namespace BusinessLogic
{
  // Clients have no access to this class, but need its functionalities 
  internal class ElectricShaver
  {
    internal string Request(int cable)
    {
      string Status = "Not yet plug in";
      switch(cable)
      {
 case 0:
   Status = "110 Voltage goes here";
   break;
 case 1:
   Status = "220 voltage goes here";
   break;
 default:
   Status = "Kaboom";
   break;
      }
      return Status;
    }
  }

  // The VoltageAdapter is the adpater class providing access
  // to any internal or external interfaces that clients expect
  public class VoltageAdapter
  {
    private ElectricShaver adaptee = new ElectricShaver();

    public string Request(int cable)
    {
      return adaptee.Request(cable);
    }   
  }

Compile the code as below:
csc /t:library BL.cs
csc /r:BL.dll Adapter.cs

Love to hear your point of view about this pattern.

Hope it helps

Top 100 Contributor
Posts 22

Yup I agree with your definition of Adapter pattern. However there are 2 variations in adapter patterns itself. Class Adapter and Object Adapter. Object Adapter use Composition while Class Adapter use multiple Inheritance concepts. The one that is being used is object Adapter pattern.

 //Object Adapter pattern

using System;

namespace AdapterEg
{

    interface ICar
    {
        void Drive();
    }
    public class CCessna
    {
        public void Fly()
        {
                Console.WriteLine("Static runup OK, " +
                    "we're off in our C172...");
        }
    }
    public class CDrivableCessna : CCessna, ICar
    {
        public void Drive() { base.Fly(); }
    }

    public class Executor
    {
        static void Main(string[] args)
        {
            ICar oCar = new CToyota();
            Console.Write("Class Adapter:\nDriving an Automobile...");
            oCar.Drive();

            oCar = new CDrivableCessna();
            Console.Write("Driving a Cessna...");

            oCar.Drive();
        }
    }

}

    

Top 25 Contributor
Posts 184
you can also say that an adapter makes use of a facade to obscure the specifics of the object it is trying to adapt to a particular interface. most of the time the adapter simplifies the use of the object being adapted. they're all related... Wink

http://devpinoy.org/blogs/cruizer

Page 1 of 1 (3 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems