适配器模式

      我们在软件开发后期或维护期间有时会碰到这种情况:我们想使用的一个模块功能能满足要求,但这个模块对外的接口与客户端要求的接口不兼容,我们又没有足够的时间来改造这个模块。也可能在设计之初,我们用的模块是属于第三方组件,它对外的接口也可能与客户端要求的接口不兼容,而我们显然是改不了此模块的。对于这种双方都不太容易修改的情况,我们可以使用适配器模式来解决。如果不是很必要,我们可以不使用适配器模式,而是直接对系统进行重构。

     适配器模式的结构图如下:

      那么适配器对象是怎么来将源接口转换成目标接口的呢? 其实就是先实现目标接口,然后在相应方法的内部调用源接口而已。就像一个翻译官一样,让我们来看一个翻译的例子来理解:

     

//客户端
static void Main(string[] args)
{
   Player oneal=new Center("O'Neal");
   oneal.Attack();

   Player yao=new Translater("姚明");
   yao.Defense();
}

//相当于目标接口
public interface Player
{
    void Attack();
    void Defense();
}
public class Center:Player
{
    public void Attack(){ }
    public void Defense(){ }
}


//相当于源接口
public interface ForeignPlayer
{
    void 进攻();
    void 防守();
}
public class ForeignCenter:ForeignPlayer
{
    public void 进攻(){ }
    public void 防守(){ }
}


//相当于适配器类
public class  Translater:Player
{
    ForeignPlayer foreignPlayer=new  ForeignCenter();

    public void Attack()
    {
        foreignPlayer.进攻();
     }
    public void Defense()
    {
       foreignPlayer. 防守();
    }
}

 适配器模式有如下优点:

1.更好的复用性:如果功能是已有的了,只是接口不兼容,那么通过适配器模式就可以让这些功能更好的复用。

2.更好的扩展性:在实现适配器功能的时候,可以调用自己开发的功能,从而自然地扩展系统的功能。

适配器模式的缺点:

过多的使用适配器会让系统非常零乱,不容易整体把握。比如,明明看到的是调用A接口,其实内部被适配成了B接口来实现,一个系统如果太多出现这种情况,无异于一场灾难。

适配器模式的本质:转换匹配,复用功能。(转换匹配是手段,复用功能是目的)

原文地址:https://www.cnblogs.com/hanmeimei/p/4573163.html