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

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) { ... }
}
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);
}
}
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 同理,均讀取全量列表操作後寫回
}
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(); }
}
}
// 接口
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(); }
}
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("用戶已存在");
}
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 組件監聽器模式,響應式交互 |
待優化項:
本系列通過完整的書店管理系統實戰,演示了從領域建模、分層架構、設計模式應用到 GUI 開發的全流程,是 Java 初中級開發者進階的優秀實踐項目。