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
-
Adapterand
and and
-
Adapteeand
and and
-
Clientand
and and
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