值对象:
封装数据记录:
User:
package cn.hncu.app.vo; import java.io.Serializable; public class User implements Serializable{//一定要实现这个接口啊!!! private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public User() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public String toString() { return name + "," + age; } }
封装查询条件:
UserQueryVO :
封装查询条件: UserQueryVO :
Dao类(数据层):
接口UserDAO :
package cn.hncu.app.dao.dao; import java.util.Collection; import cn.hncu.app.vo.User; import cn.hncu.app.vo.UserQueryVO; public interface UserDAO { public Object[] getAllUsers(); //增删改 public boolean addUser(User user); public boolean delete(User user);//有时参数也可以用:id public boolean update(User user); //3个查询 public User getByUserId(String uuid);//查单个 public Collection<User> getAll();//查全 public Collection<User> geByCondition(UserQueryVO qvo); //只写了接口,并没有具体去应用。。。。这里主要学方法 }
工厂方法UserDaoFactory :
package cn.hncu.app.dao.factory; import cn.hncu.app.dao.dao.UserDAO; import cn.hncu.app.dao.impl.UserDaoFileIO; public class UserDaoFactory { public static UserDAO getUserDAO(){ return new UserDaoFileIO(); } }
实现类 UserDaoFileIO :
package cn.hncu.app.dao.impl; import java.util.Collection; import cn.hncu.app.dao.dao.UserDAO; import cn.hncu.app.utils.FileIO; import cn.hncu.app.vo.User; import cn.hncu.app.vo.UserQueryVO; public class UserDaoFileIO implements UserDAO{ private static final String FILE_NAME = "user.txt"; @Override public Object[] getAllUsers() { return FileIO.read(FILE_NAME); } @Override public boolean addUser(User user) { return FileIO.write(FILE_NAME, user); } @Override public boolean delete(User user) { return false; } @Override public boolean update(User user) { return false; } @Override public User getByUserId(String uuid) { return null; } @Override public Collection<User> getAll() { return null; } @Override public Collection<User> geByCondition(UserQueryVO qvo) { return null; } }
逻辑层:
接口UserEbi :
package cn.hncu.app.business.ebi; import cn.hncu.app.vo.User; public interface UserEbi { public boolean addUser(User user); public Object[] getAllUser(); }
工厂类UserEbiFactory :
package cn.hncu.app.business.factory; import cn.hncu.app.business.ebi.UserEbi; import cn.hncu.app.business.impl.UserEbo; public class UserEbiFactory { public static UserEbi getUserEbi(){ return new UserEbo(); } }