1 系统功能要求: 1.1 注册功能 该系统提供注册功能,用户必须先通过注册账号来实现登录。 1.2 登录功能 用户通过输入账号密码实现登录功能,进而对仓库管理系统进行操作和管理。 1.3 货物入库 填写新货物的基本信息,实现对新货物的入库功能。 1.4 货物出库 选择要出库的货物,实现对货物的出库功能以及可删除货物的基本信息。 1.5 货物查询 通过输入货物编号/货物名称/货架号,实现对特定货物基本信息的查询。 1.6 用户管理 可修改此账号的基本信息,修改后可撤销以及可删除本账号的基本信息,实现对用户信息的管理功能。
2 系统详细设计 2.1 单例模式 功能说明:注册界面使用了单例模式,点击注册按钮时,无论如何,都只能打开一个注册界面。如果不使用单例模式对注册窗口对象进行唯一化,则将弹出多个窗口且这些窗口显示的内容完全一致,浪费内存资源。 源代码:Register.java:
public class Register extends JFrame { private static Register register = null; public static Register getInstance() { if (register == null) register = new Register(); return register; } private Register() { ... } }UML类图: 2.2 观察者模式 功能说明:登录界面用到了观察者模式,通过自定义一个登录控件,该登录控件充当观察目标,而包含该登录控件的界面是观察者。如果登录控件对象的相关事件被用户激活,则会调用界面对象中的事件处理方法来进行处理。同一个登录控件可以运用于多个不同的项目,就像按钮控件、文本框控件一样,使用该控件的开发人员只需要在界面代码中创建一个登录控件对象,并将当前界面对象注册为观察者,同时实现事件监听接口中的事件处理方法即可,重用性和扩展性非常好。 源代码:具体目标类LoginBean(登录控件类):
public class LoginBean extends JPanel implements ActionListener { ... LoginEventListener lel; LoginEvent le; public LoginBean() { ... } public void addLoginEventListener(LoginEventListener lel) { this.lel = lel; } private void fireLoginEvent(Object object, String userName, String password) { le = new LoginEvent(btnLogin, userName, password); lel.validateLogin(le); } public void actionPerformed(ActionEvent event) { if (btnLogin == event.getSource()) { String userName = this.txtUserName.getText(); String password = this.txtPassword.getText(); fireLoginEvent(btnLogin, userName, password); } if (btnRegister == event.getSource()) { Register register = Register.getInstance(); register.setVisible(true); } } }具体观察者类Login(登录界面类):
public class Login extends JFrame implements LoginEventListener { ... public Login() { ... lb = new LoginBean(); lb.addLoginEventListener(this); ... } public void validateLogin(LoginEvent event) { String userName = event.getUserName(); String password = event.getPassword(); DataOperation dataOperarion = new CipherAdapter(); password = dataOperarion.doEncrypt(password); user = Dao.login(userName, password); if (user.getUserName().equals(userName) && !user.getUserName().equals("")) { dispose(); Stock stock = Stock.getInstance(); stock.setVisible(true); } else { JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!"); } } public static UserInfo getUser() { return user; } public static void main(String[] args) { new Login().setVisible(true); } }UML类图: 2.3 适配器模式 功能说明:账号密码用到了适配器模式,将密码加密后再存储在数据库中。此将目标类和加密适配器类进行解耦,增加了类的透明性和复用性,同时系统的灵活性和扩展性都非常好,更换加密适配器或增加新的加密适配器都非常方便,符合“开闭原则”。 源代码:目标抽象类DataOperation(数据操作类):
public abstract class DataOperation { private String password; public void setPassword(String password) { this.password = password; } public String getPassword() { return this.password; } public abstract String doEncrypt(String ps); }适配者类Caesar(数据加密类):
public final class Caesar { public String doEncrypt(String ps) { String es = ""; for (int i = 0; i < ps.length(); i++) { ... } return es; } }适配器类CipherAdapter(加密适配器类):
public class CipherAdapter extends DataOperation { private Caesar cipher; public CipherAdapter() { cipher = new Caesar(); } public String doEncrypt(String ps) { return cipher.doEncrypt(ps); } }UML类图: 2.4 外观模式 功能说明:在JDBC数据库操作中用到了外观模式。在进行数据库操作的时候,首先需要创建连接Connection对象,然后通过Connection对象创建语句Statement对象或其子类的对象,如果是数据查询语句,通过Statement对象可以获取结果集ResultSet对象。在大部分数据操作代码中都多次定义了这三个对象,本人在此部分使用外观模式简化了JDBC操作代码。 源代码:Dao.java:
public class Dao { ... private static Connection conn = null; private static Statement statement = null; public Dao() { try { if (conn == null) { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(jdbcUrl, user, password); statement = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); } else return; } catch (Exception ee) { ee.printStackTrace(); } } public static ResultSet executeQuery(String sql) { try { if (conn == null) new Dao(); return statement.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); return null; } } public static int executeUpdate(String sql) { try { if (conn == null) new Dao(); return statement.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); return -1; } } public static void close() { try { statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn = null; } } ... }UML类图: 2.5 备忘录模式 功能说明:用户信息管理模块用到了备忘录模式,在用户信息管理模块中,用户可以修改自己的各项信息,在进行了错误操作之后,用户可以进行撤销,恢复到之前的状态,使得系统更加的人性化。 源代码:原发器UserInfo(用户信息类):
public class UserInfo { ... public Memento saveMemento() { return new Memento(userName, password, tel, idCard, addr); } public void restoreMemento(Memento memento) { this.userName = memento.getUserName(); this.password = memento.getPassword(); this.tel = memento.getTel(); this.idCard = memento.getIdCard(); this.addr = memento.getAddr(); } }备忘录Memento:
class Memento { ... public Memento(String userName, String password, String tel, String idCard, String addr) { this.userName = userName; this.password = password; this.tel = tel; this.idCard = idCard; this.addr = addr; } ... }负责人Caretaker:
public class Caretaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }UML类图: 3 总结 通过对各个模块的调试,基本都实现了该系统的功能。
