上一次我们讲的样例原角色仅仅有一个,这时我们仅仅须要继承就可以。
假设有多个实现类呢,Java不支持多重继承,那么我们就又一次写一个实现类,然后把其它的类通过引用过来就可以,举例来说明:
package adapter.demo; public interface IUserInfo { //获得用户姓名 public String getUserName(); //获得家庭地址 public String getHomeAddress(); //获得手机号码 public String getMobilNumber(); //获得办公室电话 public String getOfficePhoneNum(); //获得公司职位 public String getJobPosition(); //获得家庭电话 public String getHomePhone(); }
package adapter.demo; import java.util.Map; public interface IOuterUserOfficeInfo { //工作区域信息 public Map getUserOfficeInfo(); }
package adapter.demo; import java.util.Map; public interface IOuterUserHomeInfo { //用户的家庭信息 public Map getUserHomeInfo(); }
package adapter.demo; import java.util.Map; public interface IOuterUserBaseInfo { //获得用户的基本信息 public Map getUserBaseInfo(); }
package adapter.demo; import java.util.HashMap; import java.util.Map; public class OuterUserBaseInfo implements IOuterUserBaseInfo{ @Override public Map getUserBaseInfo() { HashMap baseInfoMap = new HashMap(); baseInfoMap.put("username1 ", "name"); baseInfoMap.put("mobile1", "1234455"); return baseInfoMap; } }
package adapter.demo; import java.util.HashMap; import java.util.Map; public class OuterUserHomeInfo implements IOuterUserHomeInfo{ @Override public Map getUserHomeInfo() { HashMap baseInfoMap = new HashMap(); baseInfoMap.put("username ", "name"); baseInfoMap.put("mobile", "1234455"); return baseInfoMap; } }
package adapter.demo; import java.util.HashMap; import java.util.Map; public class OuterUserOfficeInfo implements IOuterUserOfficeInfo{ @Override public Map getUserOfficeInfo() { HashMap baseInfoMap = new HashMap(); baseInfoMap.put("username11 ", "name"); baseInfoMap.put("mobile11", "1234455"); return baseInfoMap; } }
这里是重点,看怎么使用多重继承时的适配器的用法
package adapter.demo; import java.util.Map; public class OuterUserInfo implements IUserInfo{ //源对象目标 private IOuterUserBaseInfo baseinfo = null; private IOuterUserOfficeInfo officeinfo = null; private IOuterUserHomeInfo homeinfo = null; //数据处理 private Map baseMap = null; private Map homeMap = null; private Map officeMap = null; //构造函数进行对象传递 public OuterUserInfo(IOuterUserBaseInfo _baseinfo,IOuterUserOfficeInfo _officeinfo,IOuterUserHomeInfo _homeinfo){ this.baseinfo = _baseinfo; this.officeinfo = _officeinfo; this.homeinfo = _homeinfo; this.baseMap = this.baseinfo.getUserBaseInfo(); this.homeMap = this.homeinfo.getUserHomeInfo(); this.officeMap = this.officeinfo.getUserOfficeInfo(); } @Override public String getUserName() { String homeTelNum = (String) this.homeMap.get("username"); System.out.println(homeTelNum); return homeTelNum; } @Override public String getHomeAddress() { String homeAddress = (String) this.homeMap.get("mobile"); System.out.println(homeAddress); return homeAddress; } @Override public String getMobilNumber() { String mobilNum = (String) this.homeMap.get("mobile"); return mobilNum; } @Override public String getOfficePhoneNum() { // TODO Auto-generated method stub return null; } @Override public String getJobPosition() { return null; } @Override public String getHomePhone() { // TODO Auto-generated method stub return null; } }
package adapter.demo; public class Client { public static void main(String[] args) { IOuterUserHomeInfo homeinfo = new OuterUserHomeInfo(); IOuterUserBaseInfo baseinfo = new OuterUserBaseInfo(); IOuterUserOfficeInfo office = new OuterUserOfficeInfo(); IUserInfo girl = new OuterUserInfo(baseinfo, office, homeinfo); girl.getHomeAddress(); } }