适配器模式
所有代码地址 https://gitee.com/zyxscuec/Design-pattern.git
文章目录
(1)概念
适配器模式(Adapter Pattern)是指将一个类的接口转换成客户期望的另一个接口,使
原本的接口不兼容的类可以一起工作,属于结构型设计模式。
(2)适用场景
- 1、已经存在的类,它的方法和需求不匹配(方法结果相同或相似)的情况。
- 2、适配器模式不是软件设计阶段考虑的设计模式,是随着软件维护,由于不同产品、不同厂家造成功能类似而接口不相同情况下的解决方案。有点亡羊补牢的感觉。生活中也非常的应用场景,例如电源插转换头、手机充电转换头、显示器转接头。
在中国民用电都是 220V 交流电,但我们手机使用的锂电池使用的 5V 直流电。因此,我
们给手机充电时就需要使用电源适配器来进行转换。
(3)代码示例
创建 AC220 类,表示 220V 交流电:
- AC220
package com.alibaba.design.adapterpattern.powerapapter; /** * @author zhouyanxiang * @create 2020-07-2020/7/29-21:06 */ public class AC220 { public int outputAC220V(){ int output = 220; System.out.println("输出电流" + output + "V"); return output; } }
创建 DC5 接口,表示 5V 直流电的标准:
- DC5
package com.alibaba.design.adapterpattern.powerapapter; /** * @author zhouyanxiang * @create 2020-07-2020/7/29-21:07 */ public interface DC5 { public int outoupDC5V(); }
创建电源适配器 PowerAdapter 类:
- PowerAdapter
package com.alibaba.design.adapterpattern.powerapapter; /** * @author zhouyanxiang * @create 2020-07-2020/7/29-21:07 */ public class PowerAdapter implements DC5 { private AC220 ac220; public PowerAdapter(AC220 ac220) { this.ac220 = ac220; } @Override public int outoupDC5V() { int adapterInput = ac220.outputAC220V(); int adapterOutput = adapterInput / 44; System.out.println("使用PowerAdapter输入AC:" + adapterInput + "V,输出DC:" + adapterOutput + "V"); return adapterOutput; } }
客户端测试类PowerAdapterTest
- PowerAdapterTest
package com.alibaba.design.adapterpattern.test; import com.alibaba.design.adapterpattern.powerapapter.AC220; import com.alibaba.design.adapterpattern.powerapapter.DC5; import com.alibaba.design.adapterpattern.powerapapter.PowerAdapter; /** * @author zhouyanxiang * @create 2020-07-2020/7/29-21:10 */ public class PowerAdapterTest { public static void main(String[] args) { DC5 dc5 = new PowerAdapter(new AC220()); dc5.outoupDC5V(); } }
上面的案例中,通过增加 PowerAdapter 电源适配器,实现了二者的兼容。
(4)重构第三登录自由适配的业务场景
下面我们来一个实际的业务场景,利用适配模式来解决实际问题。我们很早以前开发的老系统应该都有登录接口,但是随着业务的发展和社会的进步,单纯地依赖用户名密码登录显然不能满足用户需求了。现在,我们大部分系统都已经支持多种登录方式,如 QQ 登录、微信登录、手机登录、微博登录等等,同时保留用户名密码的登录方式。虽然登录形式丰富了,但是登录后的处理逻辑可以不必改,同样是将登录状态保存到 session,遵循开闭原则。首先创建统一的返回结果 ResultMsg 类:
package com.alibaba.design.adapterpattern.loginadapter; /** * Created by Tom. */ public class ResultMsg { private int code; private String msg; private Object data; public ResultMsg(int code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
假设老系统的登录逻辑 SiginService:
package com.alibaba.design.adapterpattern.loginadapter.v1.service; import com.alibaba.design.adapterpattern.loginadapter.Member; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom. */ public class SiginService { /** * 注册方法 * @param username * @param password * @return */ public ResultMsg regist(String username, String password){ return new ResultMsg(200,"注册成功",new Member()); } /** * 登录的方法 * @param username * @param password * @return */ public ResultMsg login(String username,String password){ return null; } }
为了遵循开闭原则,老系统的代码我们不会去修改。那么下面开启代码重构之路,先创建 Member 类:
package com.alibaba.design.adapterpattern.loginadapter; /** * Created by Tom. */ public class Member { private String username; private String password; private String mid; private String info; 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 String getMid() { return mid; } public void setMid(String mid) { this.mid = mid; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
创建一个新的类SinginForThirdService继承原来的逻辑,运行非常稳定的代码我们不去改动:
package com.alibaba.design.adapterpattern.loginadapter.v1.service; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom on 2019/3/16. */ public class SinginForThirdService extends SiginService { public ResultMsg loginForQQ(String openId){ //1、openId是全局唯一,我们可以把它当做是一个用户名(加长) //2、密码默认为QQ_EMPTY //3、注册(在原有系统里面创建一个用户) //4、调用原来的登录方法 return loginForRegist(openId,null); } public ResultMsg loginForWechat(String openId){ return null; } public ResultMsg loginForToken(String token){ //通过token拿到用户信息,然后再重新登陆了一次 return null; } public ResultMsg loginForTelphone(String telphone,String code){ return null; } public ResultMsg loginForRegist(String username,String password){ super.regist(username,null); return super.login(username,null); } }
客户端测试代码
package com.alibaba.design.adapterpattern.loginadapter.v1; import com.alibaba.design.adapterpattern.loginadapter.v1.service.SinginForThirdService; /** * Created by Tom on 2019/3/16. */ public class SiginForThirdServiceTest { public static void main(String[] args) { SinginForThirdService service = new SinginForThirdService(); service.login("tom","123456"); service.loginForQQ("sdfasdfasf"); service.loginForWechat("sdfasfsa"); } }
通过这么一个简单的适配,完成了代码兼容。当然,我们代码还可以更加优雅,根据不
同的登录方式,创建不同的 Adapter。首先,创建 LoginAdapter 接口:
package com.alibaba.design.adapterpattern.loginadapter.v2.adapters; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * 在适配器里面,这个接口是可有可无,不要跟模板模式混淆 * 模板模式一定是抽象类,而这里仅仅只是一个接口 * Created by Tom */ public interface LoginAdapter { boolean support(Object adapter); ResultMsg login(String id, Object adapter); }
分别实现不同的登录适配,QQ 登录 LoginForQQAdapter:
package com.alibaba.design.adapterpattern.loginadapter.v2.adapters; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom on 2019/3/16. */ public class LoginForQQAdapter implements LoginAdapter { @Override public boolean support(Object adapter) { return adapter instanceof LoginForQQAdapter; } @Override public ResultMsg login(String id, Object adapter) { return null; } }
新浪微博登录 LoginForSinaAdapter:
package com.alibaba.design.adapterpattern.loginadapter.v2.adapters; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom. */ public class LoginForSinaAdapter implements LoginAdapter { @Override public boolean support(Object adapter) { return adapter instanceof LoginForSinaAdapter; } @Override public ResultMsg login(String id, Object adapter) { return null; } }
手机号登录 LoginForTelAdapter:
package com.alibaba.design.adapterpattern.loginadapter.v2.adapters; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom. */ public class LoginForTelAdapter implements LoginAdapter { @Override public boolean support(Object adapter) { return adapter instanceof LoginForTelAdapter; } @Override public ResultMsg login(String id, Object adapter) { return null; } }
Token 自动登录 LoginForTokenAdapter:
package com.alibaba.design.adapterpattern.loginadapter.v2.adapters; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom. */ public class LoginForTokenAdapter implements LoginAdapter { @Override public boolean support(Object adapter) { return adapter instanceof LoginForTokenAdapter; } @Override public ResultMsg login(String id, Object adapter) { return null; } }
微信登录 LoginForWechatAdapter:
package com.alibaba.design.adapterpattern.loginadapter.v2.adapters; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * Created by Tom. */ public class LoginForWechatAdapter implements LoginAdapter { @Override public boolean support(Object adapter) { return adapter instanceof LoginForWechatAdapter; } @Override public ResultMsg login(String id, Object adapter) { return null; } }
然后,创建第三方登录兼容接口 IPassportForThird:
package com.alibaba.design.adapterpattern.loginadapter.v2; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; /** * 只扩展 * Created by Tom on 2019/3/16. */ public interface IPassportForThird { /** * QQ登录 * @param id * @return */ ResultMsg loginForQQ(String id); /** * 微信登录 * @param id * @return */ ResultMsg loginForWechat(String id); /** * 记住登录状态后自动登录 * @param token * @return */ ResultMsg loginForToken(String token); /** * 手机号登录 * @param telphone * @param code * @return */ ResultMsg loginForTelphone(String telphone, String code); /** * 注册后自动登录 * @param username * @param passport * @return */ ResultMsg loginForRegist(String username, String passport); }
实现兼容 PassportForThirdAdapter:
package com.alibaba.design.adapterpattern.loginadapter.v2; import com.alibaba.design.adapterpattern.loginadapter.ResultMsg; import com.alibaba.design.adapterpattern.loginadapter.v1.service.SiginService; import com.alibaba.design.adapterpattern.loginadapter.v2.adapters.*; /** * 结合策略模式、工厂模式、适配器模式 * Created by Tom on 2019/3/16. */ public class PassportForThirdAdapter extends SiginService implements IPassportForThird { @Override public ResultMsg loginForQQ(String id) { // return processLogin(id,RegistForQQAdapter.class); return processLogin(id, LoginForQQAdapter.class); } @Override public ResultMsg loginForWechat(String id) { return processLogin(id, LoginForWechatAdapter.class); } @Override public ResultMsg loginForToken(String token) { return processLogin(token, LoginForTokenAdapter.class); } @Override public ResultMsg loginForTelphone(String telphone, String code) { return processLogin(telphone, LoginForTelAdapter.class); } @Override public ResultMsg loginForRegist(String username, String passport) { super.regist(username,passport); return super.login(username,passport); } private ResultMsg processLogin(String key,Class<? extends LoginAdapter> clazz){ try{ //适配器不一定要实现接口 LoginAdapter adapter = clazz.newInstance(); //判断传过来的适配器是否能处理指定的逻辑 if(adapter.support(adapter)){ return adapter.login(key,adapter); } }catch (Exception e){ e.printStackTrace(); } return null; } //类图的快捷键 Ctrl + Alt + Shift + U }
客户端测试代码:
package com.alibaba.design.adapterpattern.loginadapter.v2; /** * Created by Tom. */ public class PassportTest { public static void main(String[] args) { IPassportForThird passportForThird = new PassportForThirdAdapter(); passportForThird.loginForQQ(""); } }
最后,来看一下类图:
至此,我们在遵循开闭原则的前提下,完整地实现了一个兼容多平台登录的业务场景。
(5)模式在源码中的体现
Spring 中适配器模式也应用得非常广泛,例如:SpringAOP 中的 AdvisorAdapter 类,它有三个实现类 MethodBeforeAdviceAdapter、AfterReturningAdviceAdapter 和ThrowsAdviceAdapter,先来看顶层接口 AdvisorAdapter 的源代码:
package org.springframework.aop.framework.adapter; import org.aopalliance.aop.Advice; import org.aopalliance.intercept.MethodInterceptor; import org.springframework.aop.Advisor; public interface AdvisorAdapter { boolean supportsAdvice(Advice var1); MethodInterceptor getInterceptor(Advisor var1); }
再看 MethodBeforeAdviceAdapter 类:
package org.springframework.aop.framework.adapter; import java.io.Serializable; import org.aopalliance.aop.Advice; import org.aopalliance.intercept.MethodInterceptor; import org.springframework.aop.Advisor; import org.springframework.aop.MethodBeforeAdvice; class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable { MethodBeforeAdviceAdapter() { } public boolean supportsAdvice(Advice advice) { return advice instanceof MethodBeforeAdvice; } public MethodInterceptor getInterceptor(Advisor advisor) { MethodBeforeAdvice advice = (MethodBeforeAdvice)advisor.getAdvice(); return new MethodBeforeAdviceInterceptor(advice); } }
Spring 会根据不同的 AOP 配置来确定使用对应的 Advice,跟策略模式不同的一个方法可以同时拥有多个 Advice。
(6)适配器模式的优缺点
- 优点:
1、能提高类的透明性和复用,现有的类复用但不需要改变。
2、目标类和适配器类解耦,提高程序的扩展性。
3、在很多业务场景中符合开闭原则。 - 缺点:
1、适配器编写过程需要全面考虑,可能会增加系统的复杂性。
2、增加代码阅读难度,降低代码可读性,过多使用适配器会使系统代码变得凌乱。