我正在编写我的第一个Java应用程序,它应该是一个带有Swing GUI的桌面学生管理应用程序。
DAO已经实现:使用.csv文件实现持久性的学生管理应用程序
应用程序应该提供一个登录表单。成功登录后,应该有一个不同的框架/窗口,根据每个用户角色(学生、教授.)进行不同的操作。
package com.studentenverwaltung;
import com.studentenverwaltung.controller.UserController;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.view.UserView;
class Main {
public static void main(String[] args) {
User user = new User();
UserView userView = new UserView();
UserController userController = new UserController(user, userView);
userController.login();
}
}package com.studentenverwaltung.model;
import com.studentenverwaltung.controller.UserController;
public class User {
private UserController userController;
private String id;
private String password;
public UserController getUserController() {
return this.userController;
}
public void setUserController(UserController userController) {
this.userController = userController;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean checkPassword(String password) {
if (this.password.equals(password)) {
return true;
}
return false;
}
}package com.studentenverwaltung.controller;
import com.studentenverwaltung.helpers.UserCredentials;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.persistence.FileUserDAO;
import com.studentenverwaltung.persistence.UserDAO;
import com.studentenverwaltung.view.UserView;
public class UserController {
private User user;
private UserView userView;
public UserController(User user, UserView userView) {
this.user = user;
this.userView = userView;
this.user.setUserController(this);
this.userView.setUserController(this);
}
public void login() {
UserDAO userDAO = new FileUserDAO("Files/users.csv");
UserCredentials userCredentials;
userCredentials = this.userView.loginForm();
while (userCredentials != null) {
String id = userCredentials.getId();
String password = userCredentials.getPassword();
if (userDAO.getUser(id) != null
&& userDAO.getUser(id).checkPassword(password)) {
this.userView.successfullyLoggedIn();
System.exit(0);
}
System.out.println("Try again!\n==========");
userCredentials = this.userView.loginForm();
}
}
}package com.studentenverwaltung.view;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.studentenverwaltung.controller.UserController;
import com.studentenverwaltung.helpers.UserCredentials;
public class UserView {
private UserController userController;
public UserController getUserController() {
return this.userController;
}
public void setUserController(UserController userController) {
this.userController = userController;
}
public UserCredentials loginForm() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter Id...");
String id = br.readLine();
System.out.println("Enter Password...");
String password = br.readLine();
return new UserCredentials(id, password);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void successfullyLoggedIn() {
System.out.println("You're successfully logged in...!");
}
}发布于 2013-05-07 08:06:36
class Main {
public static void main(String[] args) {
User user = new User();
UserView userView = new UserView();
UserController userController = new UserController(user, userView);
userController.login();
}
}好,短,容易理解:干得好!:)
package com.studentenverwaltung.model;警告:我将对访问器有很多了解:重要的是要考虑它们,而不是盲目地为每个值添加一个getter和setter。
import com.studentenverwaltung.controller.UserController;
public class User {
private UserController userController;在典型的MVC中,模型不需要了解控制器:相反,视图和控制器了解模型。这允许您独立测试您的模型。
private String id;
private String password;我知道这只是一个任务,但考虑哈希你的密码。
public UserController getUserController() {
return this.userController;
}
public void setUserController(UserController userController) {
this.userController = userController;
}如前所述,这是不必要的(而且未使用!)。
public void setId(String id) {
this.id = id;
}考虑您提供的访问器是很重要的。您真的想要允许更改User的id吗?
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}你想允许这样做吗?用setPassword()复制resetPassword()可能更好。getPassword()的唯一用例是checkPassword()。
public boolean checkPassword(String password) {
if (this.password.equals(password)) {
return true;
}
return false;
}checkPassword是一个不错的getter示例:它不只是返回值,而是包含一些逻辑。这种getter允许加密:您可以散列password参数,并将其与实际散列密码进行比较。还请注意,tou可以简单地编写return this.password.equals(password);。
import com.studentenverwaltung.persistence.FileUserDAO;
import com.studentenverwaltung.persistence.UserDAO;你不认为应该了解道的是模特吗?
import com.studentenverwaltung.view.UserView;
public class UserController {
private User user;
private UserView userView;
public UserController(User user, UserView userView) {
this.user = user;
this.userView = userView;
this.user.setUserController(this);有意义:控制器了解视图和用户。
this.userView.setUserController(this);只有当视图是一种可以向控制器抛出事件的GUI时,这才有意义。我不认为这里是这样的。
}
public void login() {
UserDAO userDAO = new FileUserDAO("Files/users.csv");
UserCredentials userCredentials;
userCredentials = this.userView.loginForm();
while (userCredentials != null) {在典型的MVC应用程序中,控制器不轮询视图:发送事件的是视图。然而,由于它目前只是一个命令行应用程序,这是有意义的。
String id = userCredentials.getId();
String password = userCredentials.getPassword();
if (userDAO.getUser(id) != null
&& userDAO.getUser(id).checkPassword(password)) {这个应该在模型里!MVC是关于fat模型的!:)
this.userView.successfullyLoggedIn();
System.exit(0);
}
System.out.println("Try again!\n==========");
userCredentials = this.userView.loginForm();
}
}
}import com.studentenverwaltung.controller.UserController;
public class UserView {
private UserController userController;
public UserController getUserController() {
return this.userController;
}
public void setUserController(UserController userController) {
this.userController = userController;
}所有这些在这里都是未使用的:在这个命令行应用程序中,视图没有理由知道控制器。
public UserCredentials loginForm() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter Id...");
String id = br.readLine();
System.out.println("Enter Password...");
String password = br.readLine();
return new UserCredentials(id, password);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void successfullyLoggedIn() {
System.out.println("You're successfully logged in...!");
}最后两种方法非常合理。
我希望我的评论能帮助您更好地理解MVC的工作方式。
https://codereview.stackexchange.com/questions/25883
复制相似问题