Programming - Adapter Structural Design Pattern


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

The classes and/or objects participating in this example are:


  • Targetand
    • defines the domain-specific interface that Client uses.
  • Adapterand
    and and
    • adapts the interface Adaptee to the Target interface.
  • Adapteeand
    and and
    • defines an existing interface that needs adapting.
  • Clientand
    and and
    • collaborates with objects conforming to the Target interface.
using
System;

namespace DoFactory.GangOfFour.Adapter.Structural
{

// Mainapp test application

class MainApp
{
static void Main()
{
// Create adapter and place a request
Target target = new Adapter();
target.Request();

// Wait for user
Console.Read();
}
}

// "Target"

class Target
{
public virtual void Request()
{
Console.WriteLine("Called Target Request()");
}
}

// "Adapter"

class Adapter : Target
{
private Adaptee adaptee = new Adaptee();

public override void Request()
{
// Possibly do some other work
// and then call SpecificRequest
adaptee.SpecificRequest();
}
}

// "Adaptee"

class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()");
}
}
}

Output expected:


Called SpecificRequest()


and

Home | Privacy Policy | Site Map | Links | Company | Contact Us | Arena           

We provide cutting edge software development, consultancy and web design services            

There are currently 639 user(s) with 40575 hits.