package com.test.design.adapter;
public interface IUserInfo {
public String getUserName();
public String getMobileNumber();
public String getHomeAddress();
public String getHomeTellNumber();
public String getOfficeTell();
public String getOfficeJob();
}
package com.test.design.adapter;
import java.util.HashMap;
public interface IOuterUser {
public HashMap<String, String> getBasicalInfo();
public HashMap<String, String> getHomeInfo();
public HashMap<String, String> getOfficeInfo();
}
package com.test.design.adapter;
public class UserInfo implements IUserInfo {
@Override
public String getHomeAddress() {
System.out.println("家庭地址为:....");
return "家庭地址为:....";
}
@Override
public String getHomeTellNumber() {
System.out.println("家庭号码为:....");
return "家庭号码为:....";
}
@Override
public String getMobileNumber() {
System.out.println("手机号码为:....");
return "手机号码为:....";
}
@Override
public String getOfficeJob() {
System.out.println("职位为:....");
return "职位为:....";
}
@Override
public String getOfficeTell() {
System.out.println("工位电话为:....");
return "工位电话为:....";
}
@Override
public String getUserName() {
System.out.println("姓名为:....");
return "姓名为:....";
}
}
package com.test.design.adapter;
import java.util.HashMap;
public class OuterUser implements IOuterUser {
@Override
public HashMap<String, String> getBasicalInfo() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "张三");
map.put("tell", "123456789");
return map;
}
@Override
public HashMap<String, String> getHomeInfo() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("add", "海淀区");
map.put("tell", "010-2929292");
return map;
}
@Override
public HashMap<String, String> getOfficeInfo() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("position", "Boss");
map.put("tell", "010-2333333");
return map;
}
}
package com.test.design.adapter;
import java.util.HashMap;
//适配器类(类适配)
//如果要使用对象适配,不继承OuterUser,而直接引用OuterUser对象即可
public class OuterUserInfo extends OuterUser implements IUserInfo {
private HashMap<String, String> baseInfo = super.getBasicalInfo();
private HashMap<String, String> officeInfo = super.getOfficeInfo();
private HashMap<String, String> homeInfo = super.getHomeInfo();
@Override
public String getHomeAddress() {
return homeInfo.get("add");
}
@Override
public String getHomeTellNumber() {
return homeInfo.get("tell");
}
@Override
public String getMobileNumber() {
return baseInfo.get("tell");
}
@Override
public String getOfficeJob() {
return officeInfo.get("position");
}
@Override
public String getOfficeTell() {
return officeInfo.get("tell");
}
@Override
public String getUserName() {
return baseInfo.get("name");
}
}
package com.test.design.adapter;
public class Client {
public static void main(String[] args) {
//将IOuterUser转化为了IUserInfo
IUserInfo userInfo = new OuterUserInfo();
System.out.println(userInfo.getHomeAddress());
System.out.println(userInfo.getHomeTellNumber());
System.out.println(userInfo.getMobileNumber());
System.out.println(userInfo.getUserName());
System.out.println(userInfo.getOfficeJob());
System.out.println(userInfo.getOfficeTell());
}
}
适配器模式不适合在系统设计阶段采用,没有一个系统分析师会在做详设的时候考虑使用适配器模式,这个模式使用的主要场景是扩展应用中,系统扩展了,不符合原有设计的时候才考虑通过适配器模式减少代码修改带来的风险。