首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对高校项目的反馈

对高校项目的反馈
EN

Code Review用户
提问于 2013-05-16 17:02:05
回答 1查看 915关注 0票数 5

我希望得到一些关于以下大学项目代码的最佳实践的反馈意见。

控制器里应该放什么,视图里应该放什么?如何将视图附加和移除到单个主框架上?成功登录后更改控制器?

Studentenverwaltung.java

代码语言:javascript
复制
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);
    }
}

LoginController.java

代码语言:javascript
复制
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;
    }
}

User.java

代码语言:javascript
复制
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);
    }
}

LoginView.java

代码语言:javascript
复制
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.
        }
    }
}

StudentView.java

代码语言:javascript
复制
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());
    }
}
EN

回答 1

Code Review用户

回答已采纳

发布于 2014-01-08 19:18:25

使用自封装,这将有助于应用开放-封闭原则.

Studentenverwaltung.java

使用将自动显式导入类的IDE。未来的维护人员不必猜测导入了哪些类:

代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;

避免使用.*

LoginController.java

LoginController没有任何目的。面向对象的类必须同时具有行为和属性。此外,应首先根据行为来定义类。

这通常意味着问:“LoginController是做什么的?”然后将这些责任定义为方法。属性(数据)是次要的考虑因素。在阅读“告诉我,别问”之后,这一点就更加明显了。

User.java

以下几个问题:

  • 类应尽可能通用。
  • 密码不应以明文形式储存。
  • 密码验证可能是LoginController的责任

例如,一般用户将不具有以下属性:

代码语言:javascript
复制
private String password;
private String degreeProgram;
private boolean isLeaderOfDegreeProgram;
private String course;

使用单一的角色没有必要强加一个任意的限制。如果用户可以有多个角色,那么其中一个角色可以是“学位项目负责人”。

至少密码应该是散列

LoginView.java

在文体上,大多数对this.的引用都是多余的:

代码语言:javascript
复制
this.setContentPane(contentPane);
this.setModal(true);
this.getRootPane().setDefaultButton(btnLogin);

以下内容似乎过于冗长:

代码语言:javascript
复制
LoginView.this.onLogin();

我认为可以是:

代码语言:javascript
复制
onLogin();

StudentView.java

LoginView和StudentView之间有一些可以抽象的复制:

代码语言:javascript
复制
public JPanel contentPane;
private JTextField txtId;
private JTextField txtPassword;
private User user;

它们可以在包含两个元素的泛型"View“超类中。或者,它们可能在一个公共类中,这两个类都包含在其中。

让所有变量都是私有的。没有例外。

StudentView.java

避免硬编码文本:

代码语言:javascript
复制
    this.lblWelcome.setText("Herzlich Willkommen, " + this.user.getFirstName() + " " + this.user.getLastName());

对复合消息使用ResourceBundle

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/26252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档