我希望得到一些关于以下大学项目代码的最佳实践的反馈意见。
控制器里应该放什么,视图里应该放什么?如何将视图附加和移除到单个主框架上?成功登录后更改控制器?
package com.studentenverwaltung;
import com.studentenverwaltung.controller.LoginController;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.view.LoginView;
import javax.swing.*;
import java.awt.*;
class Studentenverwaltung implements Runnable {
public static void main(String[] args) {
EventQueue.invokeLater(new Studentenverwaltung());
}
@Override
public void run() {
User user = new User();
LoginView loginView = new LoginView(user);
LoginController loginController = new LoginController(user, loginView);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(loginView);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}package com.studentenverwaltung.controller;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.view.LoginView;
public class LoginController {
private User user;
private LoginView LoginView;
public LoginController(User user, LoginView LoginView) {
this.user = user;
this.LoginView = LoginView;
}
}package com.studentenverwaltung.model;
import java.util.Observable;
public class User extends Observable {
private String lastName;
private String firstName;
private String role;
private String id;
private String password;
private String degreeProgram;
private boolean isLeaderOfDegreeProgram;
private String course;
// Getter & Setter
public boolean checkPassword(String password) {
return this.password.equals(password);
// this.setChanged();
// this.notifyObservers(this);
}
}package com.studentenverwaltung.view;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.persistence.FileUserDAO;
import javax.swing.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;
public class LoginView extends JDialog {
private JPanel contentPane;
private JButton btnLogin;
private JButton btnCancel;
private JTextField txtId;
private JPasswordField txtPassword;
private User user;
public LoginView(User user) {
this.user = user;
this.user.addObserver(new UserObserver());
this.init();
}
private void init() {
this.setContentPane(contentPane);
this.setModal(true);
this.getRootPane().setDefaultButton(btnLogin);
this.btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginView.this.onLogin();
}
});
this.btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginView.this.onCancel();
}
});
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
LoginView.this.onCancel();
}
});
this.contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginView.this.onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
/*
public static void main(String[] args) {
LoginView dialog = new LoginView();
dialog.pack();
dialog.setVisible(true);
System.exit(0);
}
*/
private void onLogin() {
FileUserDAO userDAO;
String id, password;
User user;
userDAO = new FileUserDAO("Files/stud_info.csv");
id = this.txtId.getText();
password = this.txtPassword.getText();
user = userDAO.getUser(id);
if (user != null && user.checkPassword(password)) {
this.dispose();
// switch (user.getRole()) {
// case "student":
// //
// case "lecturer":
// //
// case "professor":
// if (user.getIsLeaderOfDegreeProgram()) {
// // leader
// }
//
// // professor
// }
frame.add(new StudentView(user).contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
private void onCancel() {
this.dispose();
}
private class UserObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
}package com.studentenverwaltung.view;
import com.studentenverwaltung.model.User;
import javax.swing.*;
public class StudentView {
public JPanel contentPane;
private JLabel lblWelcome;
private JButton btnChangePassword;
private JButton btnLogout;
private JTextField txtId;
private JTextField txtPassword;
private JTextField txtDegreeProgram;
private JTable tblPerformance;
private User user;
public StudentView(User user) {
this.user = user;
// this.tblPerformance.setModel(this.user.getAllCourses());
this.init();
}
private void init() {
this.lblWelcome.setText("Herzlich Willkommen, " + this.user.getFirstName() + " " + this.user.getLastName());
this.txtId.setText(this.user.getId());
this.txtPassword.setText(this.user.getPassword());
this.txtDegreeProgram.setText(this.user.getDegreeProgram());
}
}发布于 2014-01-08 19:18:25
使用自封装,这将有助于应用开放-封闭原则.
使用将自动显式导入类的IDE。未来的维护人员不必猜测导入了哪些类:
import javax.swing.*;
import java.awt.*;避免使用.*。
LoginController没有任何目的。面向对象的类必须同时具有行为和属性。此外,应首先根据行为来定义类。
这通常意味着问:“LoginController是做什么的?”然后将这些责任定义为方法。属性(数据)是次要的考虑因素。在阅读“告诉我,别问”之后,这一点就更加明显了。
以下几个问题:
例如,一般用户将不具有以下属性:
private String password;
private String degreeProgram;
private boolean isLeaderOfDegreeProgram;
private String course;使用单一的角色没有必要强加一个任意的限制。如果用户可以有多个角色,那么其中一个角色可以是“学位项目负责人”。
至少密码应该是散列。
在文体上,大多数对this.的引用都是多余的:
this.setContentPane(contentPane);
this.setModal(true);
this.getRootPane().setDefaultButton(btnLogin);以下内容似乎过于冗长:
LoginView.this.onLogin();我认为可以是:
onLogin();LoginView和StudentView之间有一些可以抽象的复制:
public JPanel contentPane;
private JTextField txtId;
private JTextField txtPassword;
private User user;它们可以在包含两个元素的泛型"View“超类中。或者,它们可能在一个公共类中,这两个类都包含在其中。
让所有变量都是私有的。没有例外。
避免硬编码文本:
this.lblWelcome.setText("Herzlich Willkommen, " + this.user.getFirstName() + " " + this.user.getLastName());对复合消息使用ResourceBundle。
https://codereview.stackexchange.com/questions/26252
复制相似问题