适配器模式

适配器模式

什么是适配器模式?

适配器模式(Adapter)的定义如下:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。适配器模式分为类结构型模式和对象结构型模式两种,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。

该模式的主要优点如下。

  • 客户端通过适配器可以透明地调用目标接口。
  • 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
  • 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
  • 在很多业务场景中符合开闭原则。


其缺点是:

  • 适配器编写过程需要结合业务场景全面考虑,可能会增加系统的复杂性。
  • 增加代码阅读难度,降低代码可读性,过多使用适配器会使系统代码变得凌乱。

1. 模式的结构

适配器模式(Adapter)包含以下主要角色。

  1. 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
  2. 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
  3. 适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

1.类适配器模式:

//目标接口

public interface Target {

    //交流电

    public void alternatingCurrent();

}

//适配者

public class Adaptee {

    public void directCurrent(){

        System.out.println("这个是直流电");//交流被适配成直流

    }

}

//类适配器

public class ClassAdapter extends Adaptee implements Target{

    @Override

    public void alternatingCurrent(){

        super.directCurrent();//适配成直流电

    }

}

public class Test {

    public static void main(String[] args) {

        Test test = new Test();

        test.charge();

    }



    public void charge() {

        Target target = new ClassAdapter();

        System.out.println("直流电脑正在充电...");

        target.alternatingCurrent();

    }

}

2.对象适配器模式:

//目标接口

public interface Target {

    //交流电

    public void alternatingCurrent();

}

//适配者

public class Adaptee {

    public void directCurrent(){

        System.out.println("这个是直流电");//交流被适配成直流

    }

}

//对象适配器

public class ObjectAdapter implements Target{

    private Adaptee adaptee;

    public ObjectAdapter(Adaptee adaptee){

        this.adaptee = adaptee;

    }



    @Override

    public void alternatingCurrent(){

        adaptee.directCurrent();//适配成直流电

    }

}

public class Test {

    public static void main(String[] args) {

        Test test = new Test();

        test.charge();

    }



    public void charge() {

        Adaptee adaptee = new Adaptee();

        Target target = new ObjectAdapter(adaptee);

        System.out.println("直流电脑正在充电...");

        target.alternatingCurrent();

    }

}

两个不同点:

1.在适配器中,类适配器继承了被适配类Adaptee,然后通过调用父类Adaptee的方法来实现Target类中的方法,从而完成适配;

2.在适配器中,对象适配器私有化了被适配类对象Adaptee,然后在构造函数中将Adaptee作为参数传递进去,然后直接调用Adaptee中的方法实现Target类中的适配方法,从而完成适配.

 

 

拓展:
模式的扩展

适配器模式(Adapter)可扩展为双向适配器模式,双向适配器类既可以把适配者接口转换成目标接口,也可以把目标接口转换成适配者接口,其结构图如图所示。

 

package adapter;

//目标接口

interface TwoWayTarget

{

    public void request();

}

//适配者接口

interface TwoWayAdaptee

{

    public void specificRequest();

}

//目标实现

class TargetRealize implements TwoWayTarget

{

    public void request()

    {      

        System.out.println("目标代码被调用!");

    }

}

//适配者实现

class AdapteeRealize implements TwoWayAdaptee

{

    public void specificRequest()

    {      

        System.out.println("适配者代码被调用!");

    }

}

//双向适配器

class TwoWayAdapter  implements TwoWayTarget,TwoWayAdaptee

{

    private TwoWayTarget target;

    private TwoWayAdaptee adaptee;

    public TwoWayAdapter(TwoWayTarget target)

    {

        this.target=target;

    }

    public TwoWayAdapter(TwoWayAdaptee adaptee)

    {

        this.adaptee=adaptee;

    }

    public void request()

    {

        adaptee.specificRequest();

    }

    public void specificRequest()

    {      

        target.request();

    }

}

//客户端代码

public class TwoWayAdapterTest

{

    public static void main(String[] args)

    {

        System.out.println("目标通过双向适配器访问适配者:");

        TwoWayAdaptee adaptee=new AdapteeRealize();

        TwoWayTarget target=new TwoWayAdapter(adaptee);

        target.request();

        System.out.println("-------------------");

        System.out.println("适配者通过双向适配器访问目标:");

        target=new TargetRealize();

        adaptee=new TwoWayAdapter(target);

        adaptee.specificRequest();

    }

}

 

文章知识点概括来自

http://c.biancheng.net/design_pattern/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值