Difference between revisions of "Adapter"

From CDOT Wiki
Jump to: navigation, search
(Code Examples)
(Code Examples)
Line 28: Line 28:
  
 
The following Adapter pattern code example is from http://dofactory.com/Patterns/PatternAdapter.aspx
 
The following Adapter pattern code example is from http://dofactory.com/Patterns/PatternAdapter.aspx
 +
It is a chemical system responsible for interacting with a legacy chemical database
  
 
<pre>
 
<pre>

Revision as of 23:28, 25 February 2007

Adapter Design Pattern

Adapter, a Structural Pattern and also referred to as a wrapper, is most commonly used when you want to reuse a class to work with a target class but are constrained by incompatible interfaces. The idea is to convert the interface of a reusable class into an interface that your classes expect.

A good analogy for the Adapter pattern would be the adapters that are commonly used to convert the voltage of Canadian/U.S made appliances to be used in different countries. In this case, the appliance (Target) is using the adapter (Adapter) to reuse the socket (Adaptee) already in place.

There are two specific types of adapter patterns - object adapter pattern and class adapter pattern. The latter uses multiple inheritance whereas the former uses an instance of the reuseable class it wants to adapt. Between the two, object adapter pattern is the favored one due to the fact that popular languages such as Java do not support multiple inheritance.

Object Adapter Pattern

This adapter pattern uses an instance of the class it wraps. By using an instance, methods belonging to the wrapped object can be invoked.

Objectadapter diag rueen2.GIF

Class Adapter Pattern

This adapter pattern uses multiple inheritance as a means to wrap the reusable class and use its functionality.

Classadapter diag rueen.GIF

Adapter UML Diagram

UML diagram example of the Adapter pattern. Specifically, the class adapter pattern

Adapter diag.gif

Image source: http://www.dofactory.com/Patterns/Diagrams/adapter.gif (Copyright 2001 - 2007 Data & Object Factory.)

Code Examples

The following Adapter pattern code example is from http://dofactory.com/Patterns/PatternAdapter.aspx It is a chemical system responsible for interacting with a legacy chemical database

using System;

namespace DoFactory.GangOfFour.Adapter.RealWorld
{

  // MainApp test application 

  class MainApp
  {
    static void Main()
    {
      // Non-adapted chemical compound 
      Compound stuff = new Compound("Unknown");
      stuff.Display();
      
      // Adapted chemical compounds 
      Compound water = new RichCompound("Water");
      water.Display();

      Compound benzene = new RichCompound("Benzene");
      benzene.Display();

      Compound alcohol = new RichCompound("Alcohol");
      alcohol.Display();

      // Wait for user 
      Console.Read();
    }
  }

  // "Target" 

  class Compound
  {
    protected string name;
    protected float boilingPoint;
    protected float meltingPoint;
    protected double molecularWeight;
    protected string molecularFormula;

    // Constructor 
    public Compound(string name)
    {
      this.name = name;
    }

    public virtual void Display()
    {
      Console.WriteLine("\nCompound: {0} ------ ", name);
    }
  }

  // "Adapter" 

  class RichCompound : Compound
  {
    private ChemicalDatabank bank;

    // Constructor 
    public RichCompound(string name) : base(name)
    {
    }

    public override void Display()
    {
      // Adaptee 
      bank = new ChemicalDatabank();
      boilingPoint = bank.GetCriticalPoint(name, "B");
      meltingPoint = bank.GetCriticalPoint(name, "M");
      molecularWeight = bank.GetMolecularWeight(name);
      molecularFormula = bank.GetMolecularStructure(name);

      base.Display();
      Console.WriteLine(" Formula: {0}", molecularFormula);
      Console.WriteLine(" Weight : {0}", molecularWeight);
      Console.WriteLine(" Melting Pt: {0}", meltingPoint);
      Console.WriteLine(" Boiling Pt: {0}", boilingPoint);
    }
  }

  // "Adaptee" 

  class ChemicalDatabank
  {
    // The Databank 'legacy API' 
    public float 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;
    }

    public string GetMolecularStructure(string compound)
    {
      string structure = "";

      switch (compound.ToLower())
      {
        case "water" : structure = "H20"; break;
        case "benzene" : structure = "C6H6"; break;
        case "alcohol" : structure = "C2H6O2"; break;
      }
      return structure;
    }

    public double GetMolecularWeight(string compound)
    {
      double weight = 0.0;
      switch (compound.ToLower())
      {
        case "water" : weight = 18.015; break;
        case "benzene" : weight = 78.1134; break;
        case "alcohol" : weight = 46.0688; break;
      }
      return weight;
    }
  }
}

References

Other

Rueen Fiez
BTP600




--Rueen 19:41, 17 January 2007 (EST)