X.d 笔记

小Web,大世界

0%

设计模式(6):适配器模式

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

适配器模式

问题:

An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed.

意图:

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Wrap an existing class with a new interface.
  • Impedance match an old component to a new system.

代码:

例:比如原先我的服务器对外提供了一个登录接口,需要客户端通过帐号密码登录,现在我改了这个接口,增加了email的登录方式,不希望影响到客户端的正常使用,又希望客户端都知道我有了新的登录方式。可以使用适配器进行修改。

代码1:开放接口

package com.xdnote.DesignPattern.structural.adapter;

public class LoginInterface {

    public void execute(Data data){
        //这个是废代码,以前的接口就是有这一行

        //如果要执行新代码,必须先用适配器适置
        System.out.println("您使用的接口即将不再提供支持,请即时更新至新的接口");
        this.executeNew(this.adapterOldData(data));
    };

    public void executeNew(DataNew data){
        System.out.println(data.getUsername()+":登录成功"+(data.getEmail().equals("")?"您还没绑定email":"你的email是"+data.getEmail()));
    }

    //适配旧的数据
    public DataNew adapterOldData(Data data){
        DataNew newdata=new DataNew("",data.getUsername(),data.getPassword());
        return newdata;
    }
}

代码2:数据

//旧客户端使用的数据类型
public class Data {
    private String username;
    private String password;

    public Data(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
//新客户端使用的数据类型
public class DataNew {
    private String email;
    private String username;
    private String password;

    public DataNew(String email, String username, String password) {
        this.email = email;
        this.username = username;
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

代码3:客户端调用

    public static void main(String[] args){
        LoginInterface login=new LoginInterface();

        //新的客户端+新接口
        DataNew datanew=new DataNew("a@b.c","ooo","password");
        login.executeNew(datanew);

        System.out.println();
        //旧的客户端+旧接口	
        Data data=new Data("xxx","password");
        login.execute(data);
    }

小结

适配器模式 Adapter

对于此模式解决的问题(版本兼容性适配)来说,在编码中可以说是经常遇到。但此模式确不常使用,理由就是一个:懒。 基本上目前此类问题在本人所在的项目中都是靠 if else switch等去搞定的。

  • 使用频率:

上面已说,本来码农们就比较恨版本性的问题,更别说为这种问题单独做接口适配了。很多时候,个人会使用桥接模式Bridge

  • 利弊影响:

如果不是经常遇到这种问题,暂时就用if else吧,本人也觉得没太大必要。

  • 小评:

全部都升到最新版本吧,哈哈哈哈~