Pluggable Adapter Pattern

Description:

Adapter Pattern:
» The Adapter pattern joins together types that were not designed to work with each other;
» The Adapter pattern enables a system to use classes whose interfaces don't quite match its requirements.

Pluggable Adapter Pattern:
» Presence of adapter is transparent, it can be put in and taken out;
» The Pluggable adapter sorts out which object is being plugged in at the time. Once a service has been plugged in and its methods have been assigned to the delegate objects, the association lasts until another set of methods is assigned.

Use When:

» Create a reusable class to cooperate with yet-to-be-built classes.;
» Change the names of methods as called and as implemented;
» Support different sets of methods for different purposes.

//Define the Adapter Class, Adaptee and Target Classes.

//The Target class (object) represents the class containing the desired class/method behaviour.
public class Target
{
public string Estimate(int i)
{
return "Estimate is " + (int)Math.Round(i / 3.0);
}
}

//The Adaptee class (object) represents the class, in which the behaviour will be modified by the Adapter class (object).
public class Adaptee
{
public double Precise(double a, double b)
{
return a / b;
}
}

The Adapter class (object) defines two constructors, each accepting the pluggable adapter to be used.
public class Adapter
{
public Func<int, string> Request;

public Adapter(Adaptee adaptee)
{
Request = delegate(int i)
{
return "Estimate based on precision is " + (int)Math.Round(adaptee.Precise(i, 3));
};
}

public Adapter(Target target)
{
Request = target.Estimate;
}
}
Image
//Define the Adapter Class, Adaptee and Target Classes.

//The Target class (object) represents the class containing the desired class/method behaviour.
public class Target
{
public string Estimate(int i)
{
return "Estimate is " + (int)Math.Round(i / 3.0);
}
}

//The Adaptee class (object) represents the class, in which the behaviour will be modified by the Adapter class (object).
public class Adaptee
{
public double Precise(double a, double b)
{
return a / b;
}
}

The Adapter class (object) defines two constructors, each accepting the pluggable adapter to be used.
public class Adapter
{
public Func<int, string> Request;

public Adapter(Adaptee adaptee)
{
Request = delegate(int i)
{
return "Estimate based on precision is " + (int)Math.Round(adaptee.Precise(i, 3));
};
}

public Adapter(Target target)
{
Request = target.Estimate;
}
}