Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Participants
The classes and/or objects participating in this pattern are:
Target (ChemicalCompound)
defines the domain-specific interface that Client uses.
Adapter (Compound)
adapts the interface Adaptee to the Target interface.
Adaptee (ChemicalDatabank)
defines an existing interface that needs adapting.
Client (AdapterApp)
collaborates with objects conforming to the Target interface.
This structural code demonstrates the Adapter pattern which maps the interface of one class onto another so that they can work together. These incompatible classes may come from different libraries or frameworks.
class ChemicalDatabank { // The Databank 'legacy API' publicfloat GetCriticalPoint(string compound, string point) { float temperature =
0.0F
;
// Melting Point if (point == "M") { switch (compound.ToLower()) { case "water" : temperature =
0.0F
; break; case "benzene" : temperature =
5.5F
; break; case "alcohol" : temperature =
-114.1F
; break; } } // Boiling Point else { switch (compound.ToLower()) { case "water" : temperature =
100.0F
; break; case "benzene" : temperature =
80.1F
; break; case "alcohol" : temperature =
78.3F
; break; } } return temperature; }