上面查了,我发觉它是用了两个设计模式:一个是代理模式,一个是工厂模式
不知道为什么要用到两个设计模式,用一个工厂模式不是就可以弄好了吗??
初次接触设计模式,不是很懂,求指教,代码如下:

解决方案 »

  1.   

    Emp.java:
    class Emp
    {
    private int id;
    private String user;
    private String password;
    private String email;
    Emp(){ }
    Emp(String user,String password,String email){
    this.user = user;
    this.password = password;
    this.email = email;
    }
    Emp(int id,String user,String password,String email){
    this.id = id;
    this.user = user;
    this.password = password;
    this.email = email;
    }
    public void setID(int id){
    this.id = id;
    }
    public void setUser(String user){
    this.user = user;
    }
    public void setPassword(String password){
    this.password = password;
    }
    public void setEmail(String email){
    this.email = email;
    }
    public int getID(){
    return this.id;
    }
    public String getUser(){
    return user;
    }
    public String getPassword(){
    return password;
    }
    public String getEmail(){
    return email;
    }
    }DatabaseConnection.java:import java.sql.*;
    class DatabaseConnection 
    {
    private static final String DRIVER     = "org.gjt.mm.mysql.Driver";
    private static final String DBURL      = "jdbc:mysql://localhost:3306/emp";
    private static final String DBUSER    = "scott";
    private static final String DBPASSWORD = "tiger";
    private static Connection conn;
    DatabaseConnection()throws Exception{
    Class.forName(DRIVER);//驱动加载
    conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//连接数据库
    }
    public Connection getConnection(){
    return conn;
    }
    public void close()throws Exception{
    conn.close();
    }
    }IEmpDAO.java:import java.util.*;
    public interface IEmpDAO
    {
    public boolean createEmp(Emp emp)throws Exception;
    public List<Emp> findAll(String keyWord)throws Exception;
    public Emp findByID(int id)throws Exception;
    }
    EmpDAOImpl.java:
    import java.util.*;
    import java.sql.*;
    class  EmpDAOImpl implements IEmpDAO 
    {
    private Connection conn;
    PreparedStatement preStatement;
    ResultSet result;
    EmpDAOImpl(Connection conn){
    preStatement = null;
    result = null;
    this.conn = conn;
    }
    public boolean createEmp(Emp e)throws Exception{

    preStatement = conn.prepareStatement("INSERT INTO emp(user,password,email) VALUES(?,?,?)");
    preStatement.setString(1,e.getUser());
    preStatement.setString(2,e.getPassword());
    preStatement.setString(3,e.getEmail());
    preStatement.execute("use study");//先使用哪个数据库
    preStatement.execute();
    preStatement.close();
    return true;
    }
    public List<Emp> findAll(String keyWord)throws Exception{
    List <Emp> emps = new ArrayList<Emp>();
    preStatement = conn.prepareStatement("select * from emp where user like ? or password like ?");
    preStatement.setString(1,"%"+keyWord+"%");
    preStatement.setString(2,"%"+keyWord+"%");
    preStatement.execute("use study");//先使用哪个数据库
    result = preStatement.executeQuery();
    while(result.next()){
    Emp emp = new Emp(result.getInt("id"),
      result.getString("user"),
                  result.getString("password"),
      result.getString("email")
      );
    emps.add(emp);
    }
    preStatement.close();
    return emps;
    }
    public Emp findByID(int id)throws Exception{
    Emp emp = null;
    preStatement = conn.prepareStatement("select * from emp where id=?");
    preStatement.setInt(1,id);
    result = preStatement.executeQuery();
    while(result.next()){
    emp = new Emp(result.getInt("id"),
      result.getString("user"),
      result.getString("password"),
      result.getString("email")
    );
    }
    preStatement.close();
    return emp;
    }
    public boolean findEmp(String user)throws Exception{
    List <Emp> emps = new ArrayList<Emp>();
    preStatement = conn.prepareStatement("select * from emp where user=?");
    preStatement.setString(1,user);
    preStatement.execute("use study");
    result = preStatement.executeQuery();
    while(result.next()){
     String userTemp = result.getString("user");
     if(user.equals(userTemp)){
    preStatement.close();
    return true;
     }
    }
    preStatement.close();
    return false;
    }
    }EmpDAOProxy.java 
    import java.util.*;
    class EmpDAOProxy implements IEmpDAO
    {
    private DatabaseConnection dc;
    private EmpDAOImpl edi;
    public EmpDAOProxy()throws Exception{
    dc  = new DatabaseConnection();
    edi = new EmpDAOImpl(dc.getConnection());
    }
    public boolean createEmp(Emp emp){
    try
    {
    if(!edi.findEmp(emp.getUser()))
    edi.createEmp(emp);
    else
    return false;
    }
    catch (Exception e)
    {

    }
    finally
    {
    try
    {
    dc.close();
    }
    catch (Exception e)
    {
    }
    }
    return true;
    }
    public List<Emp> findAll(String keyWord){
    List<Emp> emps = new ArrayList<Emp>();
    try
    {
    emps = edi.findAll(keyWord);
    }
    catch (Exception e)
    {
    }
    return emps;
    }
    public Emp findByID(int id){
    Emp emp = null;
    try
    {
    emp = edi.findByID(id);
    }
    catch (Exception e)
    {
    }
    return emp;
    }
    }EmpDAOFactory.java
    class EmpDAOFactory 
    {
    public static EmpDAOProxy instance()throws Exception{
    return new EmpDAOProxy();
    }
    public static void main(String[] args)throws Exception 
    {
    Emp emp = new Emp("hello","aaaaaaa.0","[email protected]");
    EmpDAOFactory.instance().createEmp(emp);
    }
    }
      

  2.   

    代理和工厂是两种不同的设计思想,工厂你能明白,说明你在项目上需要用工厂来解耦两对象的,而对于代理模式,如果你使用过hibernate的延迟加载技术,你就很明白了,使用代理模式可以扩展真实对象从而达到对真实对象的一次再封装。
      

  3.   


    写五十个DAO,完成数据库功能。然后发现里面没有日志,给每个方法加日志记录功能。加完日志发现里面没有事务控制,再自己加事务控制,写完了就明白了。
      

  4.   

    用设计模式写代码,会使代码更有利于维护,业务逻辑更加清晰,这是些最基本的好处。如何你确定自己是初学阶段,还是先写个几万行JAVA代码,好好总结,再来研究这些设计模式吧。。