1、适配器模式 , 又称变压器模式, 就比如要我们常用的家用电器是220V,但是我们手机只能用3V,5V的
看代码,以登录案列
1、首先有个登录接口
public interface ILogin {
boolean loginQQ(String qqId);
boolean loginWechat(String openId);
}
// 具体要登录的地方
public class PublicLogin {
public boolean login(String userName, String password) {
return true;
}
}
2、定义每个登录都用一个适配器
public interface IAdpater {
boolean supper(Object clazz);
boolean login(String id, Object adpater);
}
// 组合,适配器接口与实际登入方法
public abstract class LoginUserPassword extends PublicLogin implements IAdpater {
public boolean loginUserpasswrd(String username, String password) {
if(null == password) {
password = “EMPTY”;
}
return super.login(username, password);
}
}
// 微信登入
public class WechatLoginAdpater extends LoginUserPassword {
@Override
public boolean supper(Object clazz) {
return clazz instanceof WechatLoginAdpater;
}
@Override
public boolean login(String id, Object adpater) {
if(!supper(adpater)) {
return false;
}
return super.loginUserpasswrd(id, null);
}
}
// qq登入
public class QQLoginAdpater extends LoginUserPassword {
@Override
public boolean supper(Object clazz) {
return clazz instanceof QQLoginAdpater ;
}
@Override
public boolean login(String id, Object adpater) {
if(!supper(adpater)) {
return false;
}
return super.loginUserpasswrd(id, null);
}
}
public class LoginAdpater implements ILogin {
@Override
public boolean loginQQ(String qqId) {
Class> calzz = WechatLoginAdpater.class;
try {
IAdpater adpater = (IAdpater) calzz.newInstance();
boolean adpaterSupper = adpater.supper(adpater);
System.out.println(“loginWechat adpaterSupper==” + adpaterSupper);
return adpater.login(qqId, adpater);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean loginWechat(String wechatId) {
Class> calzz = WechatLoginAdpater.class;
try {
IAdpater adpater = (IAdpater) calzz.newInstance();
boolean adpaterSupper = adpater.supper(adpater);
System.out.println("loginWechat adpaterSupper==" + adpaterSupper);
return adpater.login(wechatId, adpater);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
public class LoginAdpater implements ILogin {
@Override
public boolean loginQQ(String qqId) {
Class> calzz = QQLoginAdpater.class;
try {
IAdpater adpater = (IAdpater) calzz.newInstance();
boolean adpaterSupper = adpater.supper(adpater);
System.out.println(“loginWechat adpaterSupper==” + adpaterSupper);
return adpater.login(qqId, adpater);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean loginWechat(String wechatId) {
Class> calzz = WechatLoginAdpater.class;
try {
IAdpater adpater = (IAdpater) calzz.newInstance();
boolean adpaterSupper = adpater.supper(adpater);
System.out.println("loginWechat adpaterSupper==" + adpaterSupper);
return adpater.login(wechatId, adpater);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
public class Test {
public static void main(String[] args) {
LoginAdpater loginAdpater = new LoginAdpater();
boolean success = loginAdpater.loginWechat("2343242");
System.out.println(success);
boolean success1 = loginAdpater.loginQQ("aaa");
System.out.println(success1);
}
}