Convert the interface of a class into another interface clients ecpect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
目标角色接口
public interface Target {
//目标角色有自己的方法
public void request();
}
具体目标角色类
public class ConcreteTarget implements Target {
@Override
public void request() {
System.out.println("如果你需要帮助,请告诉我");
}
}
源角色类
public class Adaptee {
//原有的业务逻辑
public void doSomething() {
System.out.println("If you need help, please tell me");
}
}
适配器角色类
public class Adapter extends Adaptee implements Target {
@Override
public void request() {
super.doSomething();
}
}
场景类
public class Client {
public static void main(String[] args) {
Target target = new ConcreteTarget();
target.request();
Target target2 = new Adapter();
target2.request();
}
}
内部员工信息接口(目标)
public interface IUserInfo {
//获得用户姓名
public String getUserName();
//获得家庭地址
public String getHomeAddress();
//手机号码
public String getMobileNumber();
//办公电话
public String getOfficeTelNumber();
//员工职位
public String getJobPosition();
//家庭电话
public String getHomeNumber();
}
内部员工信息具体实现类
public class UserInfo implements IUserInfo {
@Override
public String getUserName() {
System.out.println("内部员工的名字");
return null;
}
@Override
public String getHomeAddress() {
System.out.println("内部员工的家庭地址");
return null;
}
@Override
public String getMobileNumber() {
System.out.println("内部员工的的手机号码");
return null;
}
@Override
public String getOfficeTelNumber() {
System.out.println("内部员工的办公号码");
return null;
}
@Override
public String getJobPosition() {
System.out.println("内部员工的职位");
return null;
}
@Override
public String getHomeNumber() {
System.out.println("内部员工的家庭电话");
return null;
}
}
外部员工的接口(源)
public interface IOuterInfo {
//基本信息:名字、性别、手机号码
public Map<String, String> getUserBaseInfo();
//工作区域的信息
public Map<String, String> getUserOfficeInfo();
//用户的家庭信息
public Map<String, String> getUserHomeInfo();
}
外部员工的具体实现类(源)
public class OuterUser implements IOuterInfo {
@Override
public Map<String, String> getUserBaseInfo() {
HashMap<String, String> baseInfoMap = new HashMap<String, String>();
baseInfoMap.put("userName", "员工的名字:小三");
baseInfoMap.put("mobileNumber", "员工的手机号:13423");
return baseInfoMap;
}
@Override
public Map<String, String> getUserOfficeInfo() {
HashMap<String, String> homeInfoMap = new HashMap<String, String>();
homeInfoMap.put("homeTelNumber", "员工的家庭号码是:123124");
homeInfoMap.put("homeAddress", "员工的家庭地址是:XXX");
//System.out.println(homeInfoMap.get("homeAddress"));
return homeInfoMap;
}
@Override
public Map<String, String> getUserHomeInfo() {
HashMap<String, String> officeInfoMap = new HashMap<String, String>();
officeInfoMap.put("jobPosition", "员工的职位是:BOSS");
officeInfoMap.put("officeTelNumber", "员工的办公电话是:4111314");
return officeInfoMap;
}
}
Adapter中转角色类(适配器)
public class OuterUserInfo extends OuterUser implements IUserInfo {
private Map<String, String> baseInfoMap = super.getUserBaseInfo();
private Map<String, String> homeInfoMap = super.getUserHomeInfo();
private Map<String, String> officeInfoMap = super.getUserOfficeInfo();
@Override
public String getUserName() {
String userName = this.baseInfoMap.get("userName");
System.out.println(userName);
return userName;
}
@Override
public String getHomeAddress() {
String homeAddress = this.homeInfoMap.get("homeAddress");
System.out.println(homeAddress);
return homeAddress;
}
@Override
public String getMobileNumber() {
String mobileNumber = this.baseInfoMap.get("mobileNumber");
System.out.println(mobileNumber);
return mobileNumber;
}
@Override
public String getOfficeTelNumber() {
String officeTelNumber = this.officeInfoMap.get("officeTelNumber");
System.out.println(officeTelNumber);
return officeTelNumber;
}
@Override
public String getJobPosition() {
String jobPosition = this.officeInfoMap.get("jobPosition");
System.out.println(jobPosition);
return jobPosition;
}
@Override
public String getHomeNumber() {
String homeTelNumber = this.homeInfoMap.get("homeTelNumber");
System.out.println(homeTelNumber);
return homeTelNumber;
}
}
场景类
public class Client {
public static void main(String[] args) {
System.out.println("查找内部员工的信息-------");
IUserInfo userInfo = new UserInfo();
for (int i = 0; i < 5; i++) {
userInfo.getUserName();
userInfo.getMobileNumber();
}
System.out.println("查找外部员工信息---------");
IUserInfo outerInfo = new OuterUserInfo();
for (int i = 0; i < 5; i++) {
outerInfo.getUserName();
outerInfo.getMobileNumber();
}
}
}
内部员工信息接口(目标)
public interface IUserInfo {
//获得用户姓名
public String getUserName();
//获得家庭地址
public String getHomeAddress();
//手机号码
public String getMobileNumber();
//办公电话
public String getOfficeTelNumber();
//员工职位
public String getJobPosition();
//家庭电话
public String getHomeNumber();
}
内部员工信息具体实现类
public class UserInfo implements IUserInfo {
@Override
public String getUserName() {
System.out.println("内部员工的名字");
return null;
}
@Override
public String getHomeAddress() {
System.out.println("内部员工的家庭地址");
return null;
}
@Override
public String getMobileNumber() {
System.out.println("内部员工的的手机号码");
return null;
}
@Override
public String getOfficeTelNumber() {
System.out.println("内部员工的办公号码");
return null;
}
@Override
public String getJobPosition() {
System.out.println("内部员工的职位");
return null;
}
@Override
public String getHomeNumber() {
System.out.println("内部员工的家庭电话");
return null;
}
}
外部员工的基本信息:姓名,手机号 — 接口
public interface IOuterUserBaseInfo {
//外部员工的基本信息:姓名,手机号
public Map<String, String> getUserBaseInfo();
}
外部员工的基本信息:姓名,手机号 — 具体实现类
public class OuterUserBaseInfo implements IOuterUserBaseInfo {
@Override
public Map<String, String> getUserBaseInfo() {
HashMap<String, String> userBaseInfoMap = new HashMap<String, String>();
userBaseInfoMap.put("userName", "这个员工的姓名:小三");
userBaseInfoMap.put("mobileNumber", "这个员工的手机号:1234132");
return userBaseInfoMap;
}
}
外部员工的家庭信息:家庭地址,家庭号码 — 接口
public interface IOuterUserHomeInfo {
//外部员工的家庭信息:家庭地址,家庭号码
public Map<String, String> getUserHomeInfo();
}
外部员工的家庭信息:家庭地址,家庭号码 — 实现类
public class OuterUserHomeInfo implements IOuterUserHomeInfo {
@Override
public Map<String, String> getUserHomeInfo() {
HashMap<String, String> userHomeInfoMap = new HashMap<String, String>();
userHomeInfoMap.put("homeTelNumber", "13414");
userHomeInfoMap.put("homeAddress", "XXXXXXXXXXXX");
return userHomeInfoMap;
}
}
工作区域信心:办公号码,职位 — 接口
public interface IOuterUserOfficeInfo {
//工作区域信心:办公号码,职位
public Map<String, String> getUserOfficeInfo();
}
工作区域信心:办公号码,职位 — 实现类
public class OuterUserOfficeInfo implements IOuterUserOfficeInfo {
@Override
public Map<String, String> getUserOfficeInfo() {
HashMap<String, String> userOfficeInfoMap = new HashMap<String, String>();
userOfficeInfoMap.put("jobPosition", "职位是BOSS");
userOfficeInfoMap.put("officeTelNumber", "员工的办公电话是:134124");
return userOfficeInfoMap;
}
}
Adapter(适配角色)
public class OuterUserInfo implements IUserInfo {
private IOuterUserBaseInfo baseInfo = null;
private IOuterUserHomeInfo homeInfo = null;
private IOuterUserOfficeInfo officeInfo = null;
private Map<String, String> baseInfoMap = null;
private Map<String, String> homeInfoMap = null;
private Map<String, String> officeInfoMap = null;
//构造函数传递对象(关联)
public OuterUserInfo(IOuterUserBaseInfo _baseInfo, IOuterUserHomeInfo _homeInfo, IOuterUserOfficeInfo _officeInfo) {
this.baseInfo = _baseInfo;
this.homeInfo = _homeInfo;
this.officeInfo = _officeInfo;
//数据处理
this.baseInfoMap = this.baseInfo.getUserBaseInfo();
this.homeInfoMap = this.homeInfo.getUserHomeInfo();
this.officeInfoMap = this.officeInfo.getUserOfficeInfo();
}
@Override
public String getUserName() {
String userName = this.baseInfoMap.get("userName");
System.out.println(userName);
return userName;
}
@Override
public String getHomeAddress() {
String homeAddress = this.homeInfoMap.get("homeAddress");
System.out.println(homeAddress);
return homeAddress;
}
@Override
public String getMobileNumber() {
String mobileNumber = this.baseInfoMap.get("mobileNumber");
System.out.println(mobileNumber);
return mobileNumber;
}
@Override
public String getOfficeTelNumber() {
String officeTelNumber = this.officeInfoMap.get("officeTelNumber");
System.out.println(officeTelNumber);
return officeTelNumber;
}
@Override
public String getJobPosition() {
String jobPosition = this.officeInfoMap.get("jobPosition");
System.out.println(jobPosition);
return jobPosition;
}
@Override
public String getHomeNumber() {
String homeTelNumber = this.homeInfoMap.get("homeTelNumber");
System.out.println(homeTelNumber);
return homeTelNumber;
}
}
场景类
public class Client {
public static void main(String[] args) {
System.out.println("查看内部员工信息---------");
IUserInfo userInfo = new UserInfo();
for (int i = 0; i < 3; i++) {
userInfo.getHomeAddress();
userInfo.getMobileNumber();
}
System.out.println("查看外部员工信息----------");
IOuterUserBaseInfo baseInfo = new OuterUserBaseInfo();
IOuterUserHomeInfo homeInfo = new OuterUserHomeInfo();
IOuterUserOfficeInfo officeInfo = new OuterUserOfficeInfo();
IUserInfo outerInfo = new OuterUserInfo(baseInfo, homeInfo, officeInfo);
for (int i = 0; i < 3; i++) {
outerInfo.getHomeAddress();
outerInfo.getHomeNumber();
outerInfo.getJobPosition();
outerInfo.getMobileNumber();
outerInfo.getOfficeTelNumber();
outerInfo.getUserName();
}
}
}
注:类适配器模式与对象适配器模式