黄梦莹主演的电视剧:设计模式7:Adapter Pattern (适配器模式)

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 21:22:42

设计模式7:Adapter Pattern (适配器模式)

分类: 设计模式 2009-06-25 15:53 143人阅读 评论(0) 收藏 举报

来自于:http://www.cnblogs.com/zhenyulu/articles/39386.html

参考于:http://www.dofactory.com/Patterns/PatternAdapter.aspx

一、 适配器(Adapter)模式

适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作。

名称由来

这很像变压器(Adapter),变压器把一种电压变换成另一种电压。美国的生活用电电压是110V,而中国的电压是220V。如果要在中国使用美国电器,就必须有一个能把220V电压转换成110V电压的变压器。这个变压器就是一个Adapter。

Adapter模式也很像货物的包装过程:被包装的货物的真实样子被包装所掩盖和改变,因此有人把这种模式叫做包装(Wrapper)模式。事实上,大家经常写很多这样的Wrapper类,把已有的一些类包装起来,使之有能满足需要的接口。

适配器模式的两种形式

适配器模式有类的适配器模式和对象的适配器模式两种。我们将分别讨论这两种Adapter模式。


二、 类的Adapter模式的结构:

 

由图中可以看出,Adaptee类没有Request方法,而客户期待这个方法。为了使客户能够使用Adaptee类,提供一个中间环节,即类Adapter类,Adapter类实现了Target接口,并继承自Adaptee,Adapter类的Request方法重新封装了Adaptee的SpecificRequest方法,实现了适配的目的。

因为Adapter与Adaptee是继承的关系,所以这决定了这个适配器模式是类的。

该适配器模式所涉及的角色包括:

目标(Target)角色:这是客户所期待的接口。因为C#不支持多继承,所以Target必须是接口,不可以是类。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:把源接口转换成目标接口。这一角色必须是类。


三、 类的Adapter模式示意性实现:

下面的程序给出了一个类的Adapter模式的示意性的实现:

view plain
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Adapter_Pattern  
  7. {  
  8.     //  Class Adapter pattern -- Structural example    
  9.     using System;  
  10.   
  11.     // "ITarget"  
  12.     interface ITarget  
  13.     {  
  14.         // Methods  
  15.         void Request();  
  16.     }  
  17.   
  18.     // "Adaptee"  
  19.     class Adaptee  
  20.     {  
  21.         // Methods  
  22.         public void SpecificRequest()  
  23.         {  
  24.             Console.WriteLine("Called SpecificRequest()");  
  25.         }  
  26.     }  
  27.   
  28.     // "Adapter"  
  29.     class Adapter : Adaptee, ITarget  
  30.     {  
  31.         // Implements ITarget interface  
  32.         public void Request()  
  33.         {  
  34.             // Possibly do some data manipulation  
  35.             // and then call SpecificRequest  
  36.             this.SpecificRequest();  
  37.         }  
  38.     }  
  39.   
  40.     /**/  
  41.     ///   
  42.     /// Client test  
  43.     ///   
  44.     public class Client  
  45.     {  
  46.         public static void Main(string[] args)  
  47.         {  
  48.             // Create adapter and place a request  
  49.             ITarget t = new Adapter();  
  50.             t.Request();  
  51.         }  
  52.     }  
  53. }  


四、 对象的Adapter模式的结构:

 

从图中可以看出:客户端需要调用Request方法,而Adaptee没有该方法,为了使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而将客户端与Adaptee衔接起来。由于Adapter与Adaptee是委派关系,这决定了这个适配器模式是对象的。

该适配器模式所涉及的角色包括:

目标(Target)角色:这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:通过在内部包装(Wrap)一个Adaptee对象,把源接口转换成目标接口。


五、 对象的Adapter模式示意性实现:

下面的程序给出了一个类的Adapter模式的示意性的实现:

view plain
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Adapter_Pattern  
  7. {  
  8.     // Adapter pattern -- Structural example    
  9.   
  10.     // "Target"  
  11.     class Target  
  12.     {  
  13.         // Methods  
  14.         virtual public void Request()  
  15.         {  
  16.             // Normal implementation goes here  
  17.         }  
  18.     }  
  19.   
  20.     // "Adapter"  
  21.     class Adapter : Target  
  22.     {  
  23.         // Fields  
  24.         private Adaptee adaptee = new Adaptee();  
  25.   
  26.         // Methods  
  27.         override public void Request()  
  28.         {  
  29.             // Possibly do some data manipulation  
  30.             // and then call SpecificRequest  
  31.             adaptee.SpecificRequest();  
  32.         }  
  33.     }  
  34.   
  35.     // "Adaptee"  
  36.     class Adaptee  
  37.     {  
  38.         // Methods  
  39.         public void SpecificRequest()  
  40.         {  
  41.             Console.WriteLine("Called SpecificRequest()");  
  42.         }  
  43.     }  
  44.   
  45.     /**/  
  46.     ///   
  47.     /// Client test  
  48.     ///   
  49.     public class Client  
  50.     {  
  51.         public static void Main(string[] args)  
  52.         {  
  53.             // Create adapter and place a request  
  54.             Target t = new Adapter();  
  55.             t.Request();  
  56.             Console.Read();  
  57.         }  
  58.     }  
  59. }  


六、 在什么情况下使用适配器模式

在以下各种情况下使用适配器模式:

1、 系统需要使用现有的类,而此类的接口不符合系统的需要。
2、 想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。这些源类不一定有很复杂的接口。
3、 (对对象适配器而言)在设计里,需要改变多个已有子类的接口,如果使用类的适配器模式,就要针对每一个子类做一个适配器,而这不太实际。


七、 一个实际应用Adapter模式的例子

下面的程序演示了Class Adapter与Object Adapter的应用。

view plain
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Adapter_Pattern  
  7. {  
  8.     // Example of implementing the Adapter pattern  
  9.     using System;  
  10.   
  11.     // Target  
  12.     public interface ICar  
  13.     {  
  14.         void Drive();  
  15.     }  
  16.   
  17.     // Direct use without Adapter  
  18.     public class CToyota : ICar  
  19.     {  
  20.         public void Drive()  
  21.         {  
  22.             Console.WriteLine("Vroom Vroom, we're off in our Toyota");  
  23.         }  
  24.     }  
  25.   
  26.     // Adaptee  
  27.     public class CCessna  
  28.     {  
  29.         public void Fly()  
  30.         {  
  31.             Console.WriteLine("Static runup OK, we're off in our C172");  
  32.         }  
  33.     }  
  34.   
  35.     // Class Adapter  
  36.     public class CDrivableCessna : CCessna, ICar  
  37.     {  
  38.         public void Drive() { base.Fly(); }  
  39.     }  
  40.   
  41.     // Object Adapter  
  42.     public class CDrivableCessna2 : ICar  
  43.     {  
  44.         private CCessna m_oContained;  
  45.   
  46.         public CDrivableCessna2()  
  47.         {  
  48.             m_oContained = new CCessna();  
  49.         }  
  50.   
  51.         public void Drive() { m_oContained.Fly(); }  
  52.     }  
  53.   
  54.     // Client  
  55.     public class Client  
  56.     {  
  57.         public static void Main(string[] args)  
  58.         {  
  59.             ICar oCar = new CToyota();  
  60.   
  61.             Console.Write("Class Adapter: Driving an Automobile");  
  62.             oCar.Drive();  
  63.             oCar = new CDrivableCessna();  
  64.             Console.Write("Driving a Cessna");  
  65.             oCar.Drive();  
  66.             oCar = new CDrivableCessna2();  
  67.             Console.Write(" Object Adapter: Driving a Cessna");  
  68.             oCar.Drive();  
  69.         }  
  70.     }  
  71. }  

八、 关于Adapter模式的讨论

Adapter模式在实现时有以下这些值得注意的地方:

1、 目标接口可以省略,模式发生退化。但这种做法看似平庸而并不平庸,它可以使Adaptee不必实现不需要的方法(可以参考Default Adapter模式)。其表现形式就是父类实现缺省方法,而子类只需实现自己独特的方法。这有些像模板(Template)模式。
2、 适配器类可以是抽象类。
3、 带参数的适配器模式。使用这种办法,适配器类可以根据参数返还一个合适的实例给客户端。