SignInfo.java
public class SignInfo {/*** 报名人员ID.*/private String id;/*** 考试地点.*/private String location;/*** 考试科目.*/private String subject;/*** 邮寄地址.*/private String postAddress;/*** 获取id.* @return the id*/public String getId() {return id;}/*** 设置id.* @param newId the id to set*/public void setId(String newId) {id = newId;}/*** 获取location.* @return the location*/public String getLocation() {return location;}/*** 设置location.* @param newLocation the location to set*/public void setLocation(String newLocation) {location = newLocation;}/*** 获取subject.* @return the subject*/public String getSubject() {return subject;}/*** 设置subject.* @param newSubject the subject to set*/public void setSubject(String newSubject) {subject = newSubject;}/*** 获取postAddress.* @return the postAddress*/public String getPostAddress() {return postAddress;}/*** 设置postAddress.* @param newPostAddress the postAddress to set*/public void setPostAddress(String newPostAddress) {postAddress = newPostAddress;}}
SignInfoForPool.java
public class SignInfoForPool extends SignInfo {/*** 定义一个对象池提取的key值.*/private String key;public SignInfoForPool(final String key) {this.key = key;}/*** 获取key.* @return the key*/public String getKey() {return key;}/*** 设置key.* @param newKey the key to set*/public void setKey(String newKey) {key = newKey;}}
SignInfoFactory.java
public class SignInfoFactory {/*** 池容器.*/private static Map<String, SignInfo> pool = new HashMap<>(64);public static SignInfo getSignInfo(final String key) {// 设置返回对象.SignInfo result = null;// 池中没有该对象,则建立,并放入池中.if (!pool.containsKey(key)) {System.out.println(key + "----建立对象,并放置到池中.");result = new SignInfoForPool(key);pool.put(key, result);} else {result = pool.get(key);System.out.println(key + "----直接从池中取得.");}return result;}
}
Client.java
public class Client {/*** @param args*/public static void main(String[] args) {// 初始化对象池.for (int i=0; i<4; i++) {String subject = "科目" + i;// 初始化地址.for (int j=0; j<30; j++) {String key = subject + "考试地点" + j;SignInfoFactory.getSignInfo(key);}}// 获取对象.SignInfo signInfo = SignInfoFactory.getSignInfo("科目1考试地点1");}}