Reader Level:
ARTICLE

Adaptor Design Pattern

Posted by Srihari Chinna Articles | Design & Architecture October 25, 2010
This article talks about adaptor design pattern.
  • 0
  • 0
  • 6425

Definition

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

Note: 
  1. Wrap an existing class with a new interface.
  2. Impedance match an old component to a new system
  3. Adapter, the modifier, is about creating an intermediary abstraction that translates, or maps to the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.
  4. Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an improved interface.
Design

Please refer to the code

Some of best example: Adaptor for Two pin plug, We can have only one adaptor

UML Diagram

From GoF

1.gif
 

2.gif


Code

/// <summary>
/// Target Wraper base
/// </summary>
public class AdaptorTarget
{
    public virtual void Request()
    {
        Console.WriteLine("Called Target Request()");
    }
}

/// <summary>
/// Adapter -- Wrapper
/// </summary>
public class Adaptor : AdaptorTarget
{
    internal OldSystem.Adaptee Adaptee
    {
        get { return new OldSystem.Adaptee();}
        set { ;}
    }
    public override void Request()
    {
        Adaptee.OldMethod();
    }
}

namespace OldSystem
{
    /// <summary>
    /// Adaptee -- Old system
    /// </summary>
    internal class Adaptee
    {
        internal void OldMethod()
        {
            Console.WriteLine("Called incompatable system method....");
        }
    }
}

Client

internal static AdaptorTarget AdaptorTargetLocal
{
    get
    {
        return new Adaptor();
    }
    set
    {
    }
}
static void Main(string[] args)
{
    AdaptorTargetLocal.Request();
}

Note: Adator provides wrapper for each system. Where are Façade does differently. And Proxy is also a wrapper but it exposes the same interfaces as the subsystem.


Hope this helps.

COMMENT USING

Trending up