首页 > 其他 > 详细

适配器模式

时间:2020-06-23 17:08:59      阅读:49      评论:0      收藏:0      [点我收藏+]

名称:

    适配器模式(Adapter Pattern)

    别名:包装器Wrapper

 

问题:

    The Adapter pattern acts as an intermediary between two classes, converting the interface of one class so that it can be used with the other. This enables classes with incompatible interfaces to work together.

 

解决方案:

    

1、 模式的参与者

    1、Target

    -定义Client使用的与特定领域相关的接口。

    2、Client

    -与符合Target接口的对象协同。

    3、Adaptee

    -定义一个已经存在的接口,这个接口需要适配。

    4、Adapter

    -对Adaptee的接口与Target接口进行适配。

2.实现方式

interface Target
{
    public void request();
}

 

class Adaptee
{
    public void adaptRequest()
    {       
        System.out.println("adaptRequest!");
    }
}
class ClassAdapter extends Adaptee implements Target
{
    public void request()
    {
        adaptRequest();
    }
}

 

优点和缺点:

  • 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
  • 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
  • 缺点是过多的使用适配器,会让系统非常的凌乱。
  • 单继承,只能适配一种。

 

参考资料

《设计模式:可复用面向对象软件的基础》

    

适配器模式

原文:https://www.cnblogs.com/diameter/p/13182802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!