GOF pattern - Adapter

Definition
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Frequency of use: use_medium_high.gif medium high
UML class diagram
adapter.gif
Participants
The classes and/or objects participating in the Adapter 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.
Sample code
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.
// Adapter pattern -- Structure example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;

// "Target" 
class Target {
  
// Methods
  virtual public void Request() {
    
// Normal implementation goes here
  }
}

// "Adapter" 
class Adapter : Target {
  
// Fields
  private Adaptee adaptee = new Adaptee();

  
// Methods
  override public void Request() {
    
// Possibly do some data manipulation
    
//   and then call SpecificRequest
    adaptee.SpecificRequest();
  }
}

// "Adaptee"
class Adaptee {
  
// Methods
  public void SpecificRequest() {
    Console.WriteLine(
"Called SpecificRequest()" );
  }
}

/// <summary>
///   Client test 
/// </summary>
public class Client {
  
public static void Main(string[] args) {
    
// Create adapter and place a request
    Target t = new Adapter();
    t.Request();
  }
}
This real-world code demonstrates the use of a legacy chemical databank. Chemical compound objects access the databank through an Adapter interface.
// Adapter pattern -- Real World example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;

// "Target"
class ChemicalCompound {
  
// Fields
  protected string name;
  
protected float boilingPoint;
  
protected float meltingPoint;
  
protected double molecularWeight;
  
protected string molecularFormula;

  
// Constructors
  public ChemicalCompound( string name ) {
    
this.name = name;
  }

  
// Properties
  public float BoilingPoint {
    
getreturn boilingPoint; }
  }

  
public float MeltingPoint {
    
getreturn meltingPoint;  }
  }

  
public double MolecularWeight {
    
getreturn molecularWeight; }
  }

  
public string MolecularFormula {
    
getreturn molecularFormula; }
  }
}

// "Adapter"
class Compound : ChemicalCompound {
  
// Fields
  private ChemicalDatabank bank;

  
// Constructors
  public Compound( string name ) : base( name ) {
    
// Adaptee
    bank = new ChemicalDatabank();

    
// Adaptee request methods
    boilingPoint = bank.GetCriticalPoint( name, "B" );
    meltingPoint 
= bank.GetCriticalPoint( name, "M" );
    molecularWeight 
= bank.GetMolecularWeight( name );
    molecularFormula 
= bank.GetMolecularStructure( name );
  }

  
// Methods
  public void Display() {
    Console.WriteLine(
"\nCompound: {0} ------ ",name );
    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 {
  
// Methods -- 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.5Fbreak;
        
case "alcohol": temperature = -114.1Fbreak;
      }
    } 
else {  // Boiling Point
      switch( compound.ToLower() ) {
        
case "water": temperature = 100.0F;break;
        
case "benzene" : temperature = 80.1Fbreak;
        
case "alcohol": temperature = 78.3Fbreak;
      }
    }
    
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.015break;
      
case "benzene" : weight = 78.1134break;
      
case "alcohol": weight = 46.0688break;
    }
    
return weight;
  }
}

/// <summary>
///   AdapterApp test application
/// </summary>
public class AdapterApp {
  
public static void Main(string[] args) {
    
// Retrieve and display water characteristics
    Compound water = new Compound( "Water" );
    water.Display();

    
// Retrieve and display benzene characteristics
    Compound benzene = new Compound( "Benzene" );
    benzene.Display();

    
// Retrieve and display alcohol characteristics
    Compound alcohol = new Compound( "Alcohol" );
    alcohol.Display();

    Console.Read();
  }
}

posted on 2005-07-07 08:49 毒菇求Buy 阅读(275) 评论(0)  编辑 收藏 引用 所属分类: Design PatternC#

只有注册用户登录后才能发表评论。
<2005年7月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(7)

随笔分类(133)

随笔档案(111)

文章分类(65)

文章档案(53)

相册

收藏夹(30)

BLOG

Book store

Graphics Design

搜索

最新评论

阅读排行榜

评论排行榜