Adapter Patterm简单随笔

         Adapter 模式:如何既能利用现有对象的良好实现,同时又能满足新的应用环境所要求的接口 。

        目的:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。


        对象适配器是采用对象组合而不是使用继承。对比以上两种适配方式,在类适配方式中,我们得到的适配器类具有它所继承的父类的所有的行为,同时也具有接口的所有行为,这样其实是违背了面向对象设计原则中的类的单一职责原则,而对象适配器则更符合面向对象的思想,所以在实际应用中不太推荐类适配这种方式。


        Adapter模式在.NET Framework中的一个最大的应用就是COM Interop。COM Interop就好像是COM和.NET之间的一条纽带,一座桥梁。

我们知道,COM组件对象与.NET类对象是完全不同的,但为了使COM客户程序象调用COM组件一样调用.NET对象,使.NET程序象使用.NET对象一样使用COM组件,微软在处理方式上采用了Adapter模式,对COM对象进行包装,这个包装类就是RCW(Runtime Callable Wrapper)。RCW实际上是runtime生成的一个.NET类,它包装了COM组件的方法,并内部实现对COM组件的调用。

       原先存在的操作集合:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace AdapterPatternSam
 6 {
 7     public abstract class Operater
 8     {
 9         public abstract void DoSometing();
10     }
11 }
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 
17 namespace AdapterPatternSam
18 {
19     public class OperaterA:Operater
20     {
21         public override void DoSometing()
22         {
23             Console.WriteLine("***************operater A!!!*******************");
24             Console.WriteLine();
25         }
26     }
27 }
28 
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
32 
33 namespace AdapterPatternSam
34 {
35     public class OperaterB:Operater 
36     {
37         public override void DoSometing()
38         {
39             //throw new Exception("The method or operation is not implemented.");
40             Console.WriteLine("***************operater B!!!*******************");
41             Console.WriteLine();
42         }
43     }
44 }
45 

         结构已经稳定,但是却与现在的环境却有很大差别,需要引用适配器来调解彼此之间的差别,使现在的环境能够调用原有的稳定结构。

         相对于类适配器而言:继承原有子类的所有操作的时候,还需要继承其他相关接口,所需要的代码编写量是很大的,并且违背了类的单一职责原则,采用对象适配器设计模式,采用的面向对象的思想进行设计。

         适配器代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace AdapterPatternSam
 6 {
 7     interface IAdaperater
 8     {
 9         void Work();
10     }
11 }
12 
13 
14 using System;
15 using System.Collections.Generic;
16 using System.Text;
17 
18 namespace AdapterPatternSam
19 {
20     public class Adaperater:IAdaperater
21     {
22         //采用对象组合 相对于类适配器而已,具有更好的可扩展性和维护性
23         private Operater _operater;
24 
25         public Adaperater(Operater operater) 
26         {
27             this._operater = operater;
28         }
29 
30         #region IAdaperater 成员
31 
32         public void Work()
33         {
34             //throw new Exception("The method or operation is not implemented.");
35             _operater.DoSometing();
36 
37         }
38 
39         #endregion
40     }
41 }
42 


客户端操作如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 
 6 
 7 
 8 namespace AdapterPatternSam
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //对象适配器是采用对象组合而不是使用继承,相对于类适配器而已,具有更好的松耦合和面向对象思想
15             IAdaperater adaperater = new Adaperater(new OperaterA());
16             adaperater.Work();
17 
18             Console.ReadLine();
19 
20 
21             adaperater = new Adaperater(new OperaterB());
22             adaperater.Work();
23             Console.ReadLine();
24 
25         }
26     }
27 }

操作结果如下:

********************************************************************************

源代码下载:/Files/jasenkin/AdapterPatternSam.rar

原文地址:https://www.cnblogs.com/jasenkin/p/1686093.html