<FROM Java Database Best Practise>
在各tier之间传递value的时候,不是传递的BO,也没有自行开发Data Object,而是使用所谓Mememto的Pattern,具体用HashMap作为Data Object。而在Data Object中的属性存取上,利用在BO中定义的Constant Name作为属性名,以避免拼写错误并方便编程。
Eg. BO Emplyee 及 DAO EmployeeDAO的写法。
Employee.java
package
ee.mememto;
import java.util. * ;
public class Employee ... {
public static final String EMP_ID = "emp_id";
public static final String EMP_NAME = "emp_name";
public static final String EMAIL = "email";
public static final String IS_ACTIVE = "is_active";
public static final String CREATED = "created";
private Long empId = null;
private String empName = null;
private String email = null;
private Boolean isActive = null;
private Date created = null;
public static Employee create(HashMap data)...{
Employee emp = null;
Long id = new Long(1L);
data.put(Employee.EMP_ID, id);
emp = new Employee(id,data);
return emp;
}
private Employee(Long id,HashMap data)...{
empId = id;
load(data);
}
private void load(HashMap data)...{
empName = (String)data.get(Employee.EMP_NAME);
email = (String)data.get(Employee.EMAIL);
isActive = (Boolean)data.get(Employee.IS_ACTIVE);
created = (Date)data.get(Employee.CREATED);
}
}
import java.util. * ;
public class Employee ... {
public static final String EMP_ID = "emp_id";
public static final String EMP_NAME = "emp_name";
public static final String EMAIL = "email";
public static final String IS_ACTIVE = "is_active";
public static final String CREATED = "created";
private Long empId = null;
private String empName = null;
private String email = null;
private Boolean isActive = null;
private Date created = null;
public static Employee create(HashMap data)...{
Employee emp = null;
Long id = new Long(1L);
data.put(Employee.EMP_ID, id);
emp = new Employee(id,data);
return emp;
}
private Employee(Long id,HashMap data)...{
empId = id;
load(data);
}
private void load(HashMap data)...{
empName = (String)data.get(Employee.EMP_NAME);
email = (String)data.get(Employee.EMAIL);
isActive = (Boolean)data.get(Employee.IS_ACTIVE);
created = (Date)data.get(Employee.CREATED);
}
}
EmployeeDAO.java
package
ee.mememto;
import java.util. * ;
public class EmployeeDAO ... {
public static HashMap getEmployee(long id)...{
HashMap data = new HashMap();
return data;
}
public static void create(HashMap data)...{
}
public static void save(HashMap data)...{
}
public static void remove(HashMap data)...{
}
}
import java.util. * ;
public class EmployeeDAO ... {
public static HashMap getEmployee(long id)...{
HashMap data = new HashMap();
return data;
}
public static void create(HashMap data)...{
}
public static void save(HashMap data)...{
}
public static void remove(HashMap data)...{
}
}
--END