Pluggable Adapter Pattern Code Review
Define the Adapter Class, Adaptee and Target Classes
Code Download
- Download Description:pluggable adapter pattern download
- .NET Framework:3.5
- .NET Language:C#
- Date Published:2009-07-01
- Download Size:7 KB
Code Review
- Pluggable Adapter Pattern Code Review: Define the Adapter Class, Adaptee and Target Classes
- Pluggable Adapter Pattern Code Review: Use the Pluggable Adapter Pattern
Code Walkthrough
The Adaptee class (object) represents the class, in which the behaviour will be modified by the Adapter class (object).
class Adaptee
{
public Double Precise(Double a, Double b)
{
return a / b;
}
}
The Target class (object) represents the class containing the desired class/method behaviour.
class Target
{
public String Estimate(Int32 i)
{
return "Estimate is " + (Int32)Math.Round(i/3.0);
}
}
The Adapter class (object) defines two constructors, each accepting the pluggable adapter to be used.
class Adapter
{
public Func<Int32, String> Request;
public Adapter(Adaptee adaptee) { Request = delegate(Int32 i) { return "Estimate based on precision is " + (Int32)Math.Round(adaptee.Precise(i, 3)); }; }
public Adapter(Target target) { Request = target.Estimate; } }
public Adapter(Adaptee adaptee) { Request = delegate(Int32 i) { return "Estimate based on precision is " + (Int32)Math.Round(adaptee.Precise(i, 3)); }; }
public Adapter(Target target) { Request = target.Estimate; } }