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

Java 單機書店管理系統實戰(二):分層架構設計與完整代碼實現

     編輯:Java 綜合教學

Java 單機書店管理系統實戰(二):分層架構設計與完整代碼實現

一、系統架構設計

採用經典三層架構,職責清晰分離:

  • 表現層:Swing GUI 界面,用戶交互入口
  • 業務邏輯層:EBI/EBO 模式,接口與實現分離,核心業務規則
  • 數據訪問層:DAO 模式 + 工廠模式,文件序列化持久化
  • 公共模塊:枚舉、工具類、值對象 (VO)

系統分層架構圖 包結構圖

二、核心模塊代碼解析

1. 公共枚舉:用戶類型定義

public enum UserTypeEnum {
    ADMIN(1, "超級管理員"), BOOK(2, "圖書管理員"),
    IN(3, "進貨管理員"), OUT(4, "銷售管理員"), STOCK(5, "庫存管理員");
    
    private final int type; private final String name;
    UserTypeEnum(int type, String name) { this.type = type; this.name = name; }
    public int getType() { return type; }
    public String getName() { return name; }
    public static String getNameByType(int type) { ... }
    public static int getTypeByName(String name) { ... }
}

2. 值對象:用戶模型 (可序列化、主鍵 uuid)

public class UserModel implements Serializable {
    private String uuid, name, pwd; private int type;
    // getter/setter、equals/hashCode (按 uuid)、toString
    @Override public String toString() {
        return uuid + "," + name + "," + UserTypeEnum.getNameByType(type);
    }
}

3. 數據層:文件持久化實現

public class UserDaoSerImpl implements UserDao {
    private static final String FILE_NAME = "User.txt";
    
    @Override public boolean create(UserModel user) {
        List list = FileIoUtil.readFormFile(FILE_NAME);
        if (list.stream().anyMatch(u -> u.getUuid().equals(user.getUuid()))) return false;
        list.add(user);
        FileIoUtil.write2file(list, FILE_NAME);
        return true;
    }
    // delete/update/getAll/getSingle 同理,均讀取全量列表操作後寫回
}

4. 工具類:對象序列化文件讀寫

public class FileIoUtil {
    @SuppressWarnings("unchecked")
    public static  List readFormFile(String fileName) {
        List list = new ArrayList<>();
        File file = new File(fileName);
        if (!file.exists()) return list;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
            list = (List) in.readObject();
        } catch (Exception e) { e.printStackTrace(); }
        return list;
    }
    
    public static  void write2file(List list, String fileName) {
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName))) {
            out.writeObject(list);
        } catch (Exception e) { e.printStackTrace(); }
    }
}

5. 業務層:接口與實現分離 + 工廠模式

// 接口
public interface UserEbi {
    boolean create(UserModel user);
    boolean delete(String uuid);
    boolean update(UserModel user);
    List getAll();
    List getByCondition(UserQueryModel uqm);
    UserModel getSingle(String uuid);
}

// 實現 - 純委託 DAO
public class UserEbo implements UserEbi {
    private UserDao dao = UserDaoFactory.getUserDao();
    // 所有方法直接調用 dao 對應方法
}

// 工廠 - 解耦創建邏輯
public class UserEbiFactory {
    public static UserEbi getUserEbi() { return new UserEbo(); }
}

6. 表現層:Swing 面板交互

AddPanel:添加用戶界面,驗證密碼一致性、用戶類型必選,調用 EBI 創建用戶

ListPanel:用戶列表展示,JList 綁定數據,跳轉添加頁面

// AddPanel 核心邏輯
private void btnAddActionPerformed(ActionEvent evt) {
    String uuid = tfdUuid.getText(), name = tfdName.getText();
    String pwd = new String(tfdPwd.getPassword()), pwd2 = new String(tfdPwd2.getPassword());
    if (!pwd.equals(pwd2)) { showError("密碼不一致"); return; }
    int type = UserTypeEnum.getTypeByName(combType.getSelectedItem().toString());
    
    UserModel user = new UserModel(); user.setUuid(uuid); user.setName(name); 
    user.setPwd(pwd); user.setType(type);
    
    if (UserEbiFactory.getUserEbi().create(user)) back(); else showError("用戶已存在");
}

7. 主程序啟動入口

public class BookStore extends JFrame {
    public BookStore() {
        initComponents();
        setContentPane(new ListPanel(this)); // 默認顯示列表頁
        setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new BookStore().setVisible(true));
    }
}

三、運行效果展示

用戶列表界面 添加用戶界面

四、架構亮點與改進方向

優勢說明
分層解耦EBI/EBO/DAO/Factory 模式,各層可獨立變更
枚舉規範用戶類型集中管理,避免魔法數字
序列化持久化無需數據庫,適合單機輕量級應用
GUI 事件驅動Swing 組件監聽器模式,響應式交互

待優化項:

  1. UUID 應由系統生成 (UUID.randomUUID()),而非用戶手填
  2. 文件鎖機制防併發寫入衝突
  3. 查詢條件模型 UserQueryModel 尚未實現具體查詢邏輯
  4. 密碼應加密存儲 (BCrypt/MD5+Salt)
  5. 異常處理需統一規範,避免 printStackTrace

本系列通過完整的書店管理系統實戰,演示了從領域建模、分層架構、設計模式應用到 GUI 開發的全流程,是 Java 初中級開發者進階的優秀實踐項目。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved