SimpleValue 是一个很简单的值对象,包括id,name俩个属性 public class SimpleValue { public int id; public String name; public String toString() { return name; } public boolean equals(Object o) { //按照id比较 } } 它用于保存某些复杂的值对象、对象的主建到id和关键属性,如部门名称到name中,可以理解将原来的对象中的关键属性抽取出来放到SimpleValue,这些SimpleValue将保存倒Cache中 SimpleValue也可能有其子类,一般有俩种变换 当拥有俩个健的时候,如部门id,父部门id public class TwoKeyValue extends SimpleValue { public int sid; // } 或者,当拥有俩个名称的时候,如中文名,英文名 public class TwoNameValue extends SimpleValue { public String sname; // } 这些都将放到cache中或者某个数据结构中,以方便在展现的时候调用.下面,讲用SimpleValue实现上面展现用户详细信息的例子 首先得完成Cache,这是使用SimpleValue很重要的一步 public class Cache { private static Cache cache = new Cache(); private Map deptMap = null; private Cache() { loadDepts(); } public Cache instance() { return cache; } //装栽所有部门信息 private void loadDepts() { Department[] all = Department.getAllDept(); deptMap = new HashMap() SimpleValue sv = null; for(int i=0;i<all.length;i++) { sv = new SimpleValue(); sv.id = all[i].id; sv.name = all[i].name; deptMap.put(new Integer(sv.id),sv); } } //根据部门id得到部门名称 public SimpleValue getDept(int id) { return deptMap.get(new Integer(id)); } //得到所有的部门 public SimpleValue[] allDepts() { } //刷新Cache,比如,当管理员修改一个部门的时候,需要同步Cache public void reloadDept() { synchronized(this) { loadDept(); } } } 在展现层,就可以使用SimpleValue来帮助你显示用户详细信息了 <p>id:<%=employee.id%></p> <p>name:<%=employee.name%></p> <p>dept:<%=Cache.instance().getDept(employee.dptId)%></p>

|