设计模式-适配器模式之登录
正常的登录功能【简化】:
1、设置接口返回:
package com.jdwa.adapter;
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;
}
}
2、登录逻辑:
package com.jdwa.adapter;
public class PassportService {
public ResultMsg regist(String username,String password){
System.out.println("registSucess!");
return new ResultMsg(200,"regist Success!",new Member());
}
public ResultMsg login(String username,String password){
System.out.println("username:"+username+";password:"+password);
return null;
}
}
使用适配器
为了遵循开闭原则,老系统的代码不做修改,下面开始重构:
1、创建Member类:
package com.jdwa.adapter;
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;
}
}
2、创建Target角色IPassportForThird:
package com.jdwa.adapter;
public interface IPassportForThird {
ResultMsg loginForQQ(String openId);
ResultMsg loginForWeChat(String openId);
ResultMsg loginForToken(String token);
ResultMsg loginForTelPhone(String phone,String code);
}
3、创建适配器角色兼容,继承原来的逻辑
package com.jdwa.adapter;
public class PassportForThirdAdapter extends PassportService implements IPassportForThird{
@Override
public ResultMsg loginForQQ(String openId) {
System.out.println("loginForQQ:"+openId);
return null;
}
@Override
public ResultMsg loginForWeChat(String openId) {
System.out.println("loginForWeChat:"+openId);
return null;
}
@Override
public ResultMsg loginForToken(String token) {
System.out.println("loginForToken:"+token);
return null;
}
@Override
public ResultMsg loginForTelPhone(String phone, String code) {
System.out.println("loginForTelPhone:"+phone+"-->"+code);
return null;
}
}
Client:
package com.jdwa.adapter;
public class Client {
public static void main(String[] args) {
PassportForThirdAdapter adapter = new PassportForThirdAdapter();
adapter.login("czz","123");
adapter.loginForQQ("QQ_OPenId");
adapter.loginForTelPhone("phoneNum","457856");
adapter.loginForToken("token");
adapter.loginForWeChat("wechat");
}
}
username:czz;password:123
loginForQQ:QQ_OPenId
loginForTelPhone:phoneNum-->457856
loginForToken:token
loginForWeChat:wechat
优化使用适配器的代码
通过创建不同的Adapter来实现
1、创建ILoginAdapter接口,规范所有登录适配器的接口
package com.jdwa.adapterv2;
public interface ILoginAdapter {
boolean support(Object adapter);
ResultMsg login(String id,Object adapter);
}
2、创建一个AbstractorAdapter继承PassportService原有的功能,同时实现ILoginAdapter
package com.jdwa.adapterv2;
public abstract class AbstractAdapter extends PassportService implements ILoginAdapter {
protected ResultMsg loginForRegist(String datainfo,String msg,boolean support){
System.out.println("*********************************************");
if (!support) new ResultMsg(500,"服务器错误","请联系管理人员");;
System.out.println("通过"+msg+"登陆成功:"+datainfo);
return new ResultMsg(200,"通过"+msg+"登录成功","model信息");
}
}
3、创建QQ等登录适配器:篇幅有限,就只举QQ和手机两个例子了
package com.jdwa.adapterv2;
public class LoginForQQAdapter extends AbstractAdapter {
@Override
public boolean support(Object adapter) {
return adapter instanceof LoginForQQAdapter;
}
@Override
public ResultMsg login(String openId, Object adapter) {
return super.loginForRegist(openId,"QQ",support(adapter));
}
}
package com.jdwa.adapterv2;
public class LoginForTelAdapter extends AbstractAdapter {
@Override
public boolean support(Object adapter) {
return adapter instanceof LoginForTelAdapter;
}
@Override
public ResultMsg login(String openId, Object adapter) {
return super.loginForRegist(openId,"手机",support(adapter));
}
}
4、创建适配器PassportForThirdAdapter:
package com.jdwa.adapterv2;
public class PassportForThirdAdapter implements IPassportForThird{
@Override
public ResultMsg
loginForQQ(String openId) {
return processLogin(openId,LoginForQQAdapter.class);
}
@Override
public ResultMsg loginForWeChat(String openId) {
return processLogin(openId,LoginForWechatAdapter.class);
}
@Override
public ResultMsg loginForToken(String token) {
return processLogin(token,LoginForTokenAdapter.class);
}
@Override
public ResultMsg loginForTelPhone(String phone, String code) {
return processLogin(phone+"->"+code,LoginForTelAdapter.class);
}
private ResultMsg processLogin(String id , Class<? extends ILoginAdapter> clazz){
try {
ILoginAdapter adapter = clazz.newInstance();
return adapter.login(id,adapter);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
5、测试
package com.jdwa.adapterv2;
public class Client {
public static void main(String[] args) {
IPassportForThird adapter = new PassportForThirdAdapter();
adapter.loginForQQ("45785678");
adapter.loginForTelPhone("15112345678","12345678");
}
}
*********************************************
通过QQ登陆成功:45785678
*********************************************
通过手机登陆成功:15112345678->12345678
类图:
欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!
------愿来生只做陌上的看花人,无须入尘缘,仅行于陌上,看一川风花,无爱无伤-----