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.
Class Adapter Pattern: » Simple and versatile, invisible to the client; » A class adapter implements an interface and inherits a class; » Overriding behaviour can be done more easily with a Class adapter.
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 Class and Target Interface.
//Define the ITarget interface which defines the class structure that the client will use. public interfaceITarget { string Request(int i); }
//The Adaptee class (object) represents the class, in which the behaviour will be modified by the Adapter class (object). public classAdaptee { public double SpecificRequest(double a, double b) { return a / b; } }
//The Adapter class (object) inherits behaviour from both the ITarget interface and the Adaptee class. public classAdapter : Adaptee, ITarget { public string Request(int i) { return"Rough estimate is " + (int)Math.Round(SpecificRequest(i, 3)); } }
//Define the Adapter Class, Adaptee Class and Target Interface.
//Define the ITarget interface which defines the class structure that the client will use. public interfaceITarget { string Request(int i); }
//The Adaptee class (object) represents the class, in which the behaviour will be modified by the Adapter class (object). public classAdaptee { public double SpecificRequest(double a, double b) { return a / b; } }
//The Adapter class (object) inherits behaviour from both the ITarget interface and the Adaptee class. public classAdapter : Adaptee, ITarget { public string Request(int i) { return"Rough estimate is " + (int)Math.Round(SpecificRequest(i, 3)); } }