| 名称 | Adapter | 
						
								| 结构 |   
 
 | 
						
								| 意图 | 将一个类的接口转换成客户希望的另外一个接口。A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 | 
						
								| 适用性 | 
												你想使用一个已经存在的类,而它的接口不符合你的需求。 
你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。 
(仅适用于对象A d a p t e r )你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。  | 
						
								|  |  | 
				
		
		Code Example
OurAdapter聚集了FrameworkXTraget 和FrameworkYTarget的功能. namespace Adapter_DesignPattern
namespace Adapter_DesignPattern


 {
{
 using System;
    using System;

 class FrameworkXTarget
    class FrameworkXTarget 

 
     {
{
 virtual public void SomeRequest(int x)
        virtual public void SomeRequest(int x)

 
         {
{
 // normal implementation of SomeRequest goes here
            // normal implementation of SomeRequest goes here                    
 }
        }
 }
    }

 class FrameworkYAdaptee
    class FrameworkYAdaptee

 
     {
{
 public void QuiteADifferentRequest(string str)
        public void QuiteADifferentRequest(string str) 

 
         {
{
 Console.WriteLine("QuiteADifferentRequest = {0}", str);
            Console.WriteLine("QuiteADifferentRequest = {0}", str);
 }
        }        
 }
    }

 class OurAdapter : FrameworkXTarget
    class OurAdapter : FrameworkXTarget

 
     {
{
 private FrameworkYAdaptee adaptee = new FrameworkYAdaptee();
        private FrameworkYAdaptee adaptee = new FrameworkYAdaptee();
 override public void SomeRequest(int a)
        override public void SomeRequest(int a)

 
         {
{
 string b;
            string b;
 b = a.ToString();
            b = a.ToString();
 adaptee.QuiteADifferentRequest(b);
            adaptee.QuiteADifferentRequest(b);
 }
        }        
 }
    }


 /**//// <summary>
    /**//// <summary>
 ///    Summary description for Client.
    ///    Summary description for Client.
 /// </summary>
    /// </summary>
 public class Client
    public class Client

 
     {
{
 void GenericClientCode(FrameworkXTarget x)
        void GenericClientCode(FrameworkXTarget x)

 
         {
{
 // We assume this function contains client-side code that only
            // We assume this function contains client-side code that only 
 // knows about FrameworkXTarget.
            // knows about FrameworkXTarget.
 x.SomeRequest(4);
            x.SomeRequest(4);
 // other calls to FrameworkX go here
            // other calls to FrameworkX go here
 //
            // 
 }
        }
 
        
 public static int Main(string[] args)
        public static int Main(string[] args)

 
         {
{
 Client c = new Client();
            Client c = new Client();
 FrameworkXTarget x = new OurAdapter();
            FrameworkXTarget x = new OurAdapter();
 c.GenericClientCode(x);
            c.GenericClientCode(x);    
 return 0;
            return 0;
 }
        }
 }
    }
 }
}

 // Adapter pattern -- Structural example
// Adapter pattern -- Structural example  


 using System;
using System;

 namespace DoFactory.GangOfFour.Adapter.Structural
namespace DoFactory.GangOfFour.Adapter.Structural


 {
{

 // Mainapp test application
  // Mainapp test application 

 class MainApp
  class MainApp

 
   {
{
 static void Main()
    static void Main()

 
     {
{
 // Create adapter and place a request
      // Create adapter and place a request 
 Target target = new Adapter();
      Target target = new Adapter();
 target.Request();
      target.Request();

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

 // "Target"
  // "Target" 

 class Target
  class Target

 
   {
{
 public virtual void Request()
    public virtual void Request()

 
     {
{
 Console.WriteLine("Called Target Request()");
      Console.WriteLine("Called Target Request()");
 }
    }
 }
  }

 // "Adapter"
  // "Adapter" 

 class Adapter : Target
  class Adapter : Target

 
   {
{
 private Adaptee adaptee = new Adaptee();
    private Adaptee adaptee = new Adaptee();

 public override void Request()
    public override void Request()

 
     {
{
 // Possibly do some other work
      // Possibly do some other work 
 // and then call SpecificRequest
      // and then call SpecificRequest 
 adaptee.SpecificRequest();
      adaptee.SpecificRequest();
 }
    }
 }
  }

 // "Adaptee"
  // "Adaptee" 

 class Adaptee
  class Adaptee

 
   {
{
 public void SpecificRequest()
    public void SpecificRequest()

 
     {
{
 Console.WriteLine("Called SpecificRequest()");
      Console.WriteLine("Called SpecificRequest()");
 }
    }
 }
  }
 }
}
 
 

上面例子可以认为是
对象的Adapter模式, 因为target为一个实际的类,不是一个接口
以下为
类的Adapter设计模式: 因为target 为一个接口,又接口规范,且没有实例成员
 //  Class Adapter pattern -- Structural example
//  Class Adapter pattern -- Structural example  
 using System;
using System;

 // "ITarget"
// "ITarget"
 interface ITarget
interface ITarget


 {
{
 // Methods
  // Methods
 void Request();
  void Request();
 }
}

 // "Adaptee"
// "Adaptee"
 class Adaptee
class Adaptee


 {
{
 // Methods
  // Methods
 public void SpecificRequest()
  public void SpecificRequest()

 
   {
{
 Console.WriteLine("Called SpecificRequest()" );
    Console.WriteLine("Called SpecificRequest()" );
 }
  }
 }
}

 // "Adapter"
// "Adapter"
 class Adapter : Adaptee, ITarget
class Adapter : Adaptee, ITarget


 {
{
 // Implements ITarget interface
  // Implements ITarget interface
 public void Request()
  public void Request()

 
   {
{
 // Possibly do some data manipulation
    // Possibly do some data manipulation
 // and then call SpecificRequest
    // and then call SpecificRequest
 this.SpecificRequest();
    this.SpecificRequest();
 }
  }
 }
}


 /**//**//**//// <summary>
/**//**//**//// <summary>
 /// Client test
/// Client test
 /// </summary>
/// </summary> public class Client
public class Client


 {
{
 public static void Main(string[] args)
  public static void Main(string[] args)

 
   {
{
 // Create adapter and place a request
    // Create adapter and place a request
 ITarget t = new Adapter();
    ITarget t = new Adapter();
 t.Request();
    t.Request();
 }
  }
 }
}
         类适配器提供了接口规范,
         但是对象适配器没有,如果target改变接口的话,adaper可能没有跟着改变,最后导致error.也又可能会错用target里的实例成员.