首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

DAO 與 Factory 模式實戰範例:資料存取層抽象與多實作切換

     編輯:Java 程式設計入門

領域模型

// Student.java
public class Student implements Serializable {
    private String id, name;
    private Date birthday;
    // getter/setter 省略
}

// Course.java
public class Course implements Serializable {
    private String id, name, description;
    // getter/setter 省略
}

DAO 介面定義

public interface SMdao {
    Student createStudent(String id, String name, Date birthday);
    Course createCourse(String id, String name, String description);
    Student findStudent(String id);
    Course findCourse(String id);
    void clean();
}

兩種實作策略

檔案儲存實作

public class FileSMdaoImpl implements SMdao {
    private Properties pro;
    private static final String SFILE_URL = "student.file.url";
    // 注意:原碼有筆誤 FILE_URL 應為 SFILE_URL

    public Student createStudent(...) {
        // 使用 ObjectOutputStream 序列化寫入檔案
        // 完整 try-catch-finally 關閉資源
    }
    // 其他方法暫回傳 null
}

資料庫儲存實作

public class DBSMdaoImpl implements SMdao {
    private Connection con;
    // 從 Properties 讀取 db.driver, db.url, db.user, db.passWord
    // 建立連線,使用 PreparedStatement 防 SQL Injection
    public Student createStudent(...) {
        // INSERT 參數化查詢,成功回傳 Student 物件
    }
    public void clean() { if (con != null) con.close(); }
}

Factory 封裝建立邏輯

public class SMdaoFactory {
    public static final String TYPE_FILE = "file";
    public static final String TYPE_DB = "database";
    private static SMdaoFactory factory = new SMdaoFactory();
    private String type;
    private Properties pro;

    private SMdaoFactory() {}
    public static SMdaoFactory newInstance() { return factory; }
    public void setType(String type) { this.type = type; }
    public void setProperties(Properties pro) { this.pro = pro; }

    public SMdao createSMdao() {
        if (TYPE_FILE.equals(type)) return new FileSMdaoImpl(pro);
        else return new DBSMdaoImpl(pro);
    }
}

主程式整合測試

public class Main {
    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.load(Main.class.getResourceAsStream("conf.properties"));

        SMdaoFactory factory = SMdaoFactory.newInstance();
        factory.setType(SMdaoFactory.TYPE_DB);
        factory.setProperties(pro);
        SMdao dao = factory.createSMdao();

        // 建立日期
        GregorianCalendar cal = new GregorianCalendar();
        cal.set(1975, 2, 21);  // 月份 0-based
        Date birthday = new java.sql.Date(cal.getTimeInMillis());

        Student stu = dao.createStudent("12345", "George", birthday);
    }
}

設定檔 conf.properties

db.driver=COM.pointbase.jdbc.jdbcUniversalDriver
db.url=jdbc:pointbase:server://george/sample
db.user=pbpublic
db.password=pbpublic
student.file.url=student
course.file.url=course

設計亮點總結

  • 介面隔離:上層僅依賴 SMdao 介面
  • 實作切換:透過 Factory + 設定檔,無需修改代碼即可切換檔案/資料庫儲存
  • 資源管理:clean() 統一釋放連線/串流
  • 參數化查詢:資料庫實作使用 PreparedStatement 防注入
  • 序列化儲存:檔案實作採用 Java 序列化機制
原碼修正提醒:FileSMdaoImpl 中 FILE_URL 應改為 SFILE_URL;GregorainCalendar 拼寫錯誤應為 GregorianCalendar;Properites 應為 Properties。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved