设计模式----适配器模式(adapter)

IUserInfo.java代码如下:
package com.designPattern.adapter;

/**
 * 公司员工属性
 * @author yangjianzhou
 *
 */
public interface IUserInfo {

	public String getUserName();
	
	public String getHomeAddress();
	
	public String getMobileNumber();
	
	public String getOfficeTelNumber();
	
	public String getJobPosition();
	
	public String getHomeTelNumber();
}



UserInfo.java代码如下:
package com.designPattern.adapter;

/**
 * 公司内部正式员工
 * @author yangjianzhou
 *
 */
public class UserInfo implements IUserInfo{

	@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 getHomeTelNumber() {
		System.out.println("这是员工的家庭电话!");
		return null;
	}

	@Override
	public String getUserName() {
		System.out.println("这是员工的姓名!");
		return null;
	}

	@Override
	public String getHomeAddress() {
		System.out.println("这是员工的家庭住址!");
		return null;
	}
	
}



IOuterUser.java代码如下:
package com.designPattern.adapter;

import java.util.Map;

public interface IOuterUser {

	public Map<String,String> getUserBaseInfo();
	
	public Map<String,String> getUserOfficeInfo();
	
	public Map<String,String> getUserHomeInfo();
}



OuterUser.java代码如下:
package com.designPattern.adapter;

import java.util.HashMap;
import java.util.Map;

public class OuterUser implements IOuterUser{

	@Override
	public Map<String,String> getUserBaseInfo() {
		HashMap<String,String> baseInfoMap = new HashMap<String,String>();
		baseInfoMap.put("username", "员工姓名!");
		baseInfoMap.put("mobileNumber", "员工电话!");
		
		return baseInfoMap;
	}

	@Override
	public Map<String,String> getUserOfficeInfo() {
		HashMap<String,String> officeInfoMap = new HashMap<String,String>();
		officeInfoMap.put("jobPosition", "员工的职位!");
		officeInfoMap.put("officeTelNumber", "员工的办公电话!");
		
		return officeInfoMap;
	}

	@Override
	public Map<String,String> getUserHomeInfo() {
		HashMap<String,String> homeInfoMap = new HashMap<String,String>();
		homeInfoMap.put("homeTelNumber", "员工的家庭电话!");
		homeInfoMap.put("homeAddress", "员工的家庭地址!");
		return homeInfoMap;
	}

}



OuterUserInfo.java代码如下:
package com.designPattern.adapter;

import java.util.Map;

/**
 * 适配器模式实现类
 * @author yangjianzhou
 *
 */
public class OuterUserInfo extends OuterUser implements IUserInfo{

	private Map<String,String> baseInfo = super.getUserBaseInfo();
	
	private Map<String,String> homeInfo = super.getUserHomeInfo();
	
	private  Map<String,String> officeInfo = super.getUserOfficeInfo();
	
	@Override
	public String getUserName() {
		String username = baseInfo.get("username");
		System.out.println(username);
		return username;
	}

	@Override
	public String getHomeAddress() {
		String address = homeInfo.get("homeAddress");
		System.out.println(address);
		return address;
	}

	@Override
	public String getMobileNumber() {
		String mobileNumber = baseInfo.get("mobileNumber");
		System.out.println(mobileNumber);
				
		return mobileNumber;
	}

	@Override
	public String getOfficeTelNumber() {
		String officeTelNumber = officeInfo.get("officeTelNumber");
		System.out.println(officeTelNumber);
		return officeTelNumber;
	}

	@Override
	public String getJobPosition() {
		String jobPosition = officeInfo.get("jobPosition");
		System.out.println(jobPosition);
		return jobPosition;
	}

	@Override
	public String getHomeTelNumber() {
		String homeTelNumber = homeInfo.get("homeTelNumber");
		System.out.println(homeTelNumber);
		return homeTelNumber;
	}
	
	public String toString(){
		return this.getUserName()+" : "+this.getMobileNumber()+":"+this.getHomeAddress()+":"+this.getHomeTelNumber()
				+":"+this.getOfficeTelNumber()+":"+this.getJobPosition();
		
	}

}



App.java代码如下:
package com.designPattern.adapter;

public class App {

	public static void main(String[] args) {

		IUserInfo userInfo = new OuterUserInfo();

		System.out.println(userInfo);
	}

}



运行结果如下:
员工姓名!
员工电话!
员工的家庭地址!
员工的家庭电话!
员工的办公电话!
员工的职位!
员工姓名! : 员工电话!:员工的家庭地址!:员工的家庭电话!:员工的办公电话!:员工的职位!

你可能感兴趣的:(Adapter)