简体   繁体   English

如何在Java项目中构造类

[英]How to structure my classes in Java project

Recently I began an adventure with JAVA. 最近,我开始了JAVA的冒险之旅。 I am looking for a guidance on design patterns and overall architecture of JAVA project. 我正在寻找有关JAVA项目的设计模式和总体架构的指南。

I'm working on a small application project as university assignment. 我正在做一个小型应用项目,作为大学作业。 It's a typical learning-by-doing project. 这是一个典型的边做边学的项目。 The app has GUI made up of Swing elements and few additional classes. 该应用程序的GUI由Swing元素和少量其他类组成。

The problem that I face is realted with lack of idea of how to tie it all together. 我所面对的问题是缺乏如何将所有问题联系在一起的想法。 Specifically I face following problems: 具体来说,我面临以下问题:

Problem 1 问题1

Login problem - I have custom User class. 登录问题-我有自定义的User类。 I keep the current_user as property of main JFrame . 我将current_user保留为主JFrame的属性。

During GUI loading - I would like to check if the user is signed in. Also - on specific actions - I would like to sign out user. 在GUI加载期间-我想检查用户是否登录。此外-关于特定操作-我想注销用户。

Currently I solve both problems by going all the way to mainFrame.current_user property. 目前,我一直通过mainFrame.current_user属性来解决这两个问题。 But I feel that's bad approach. 但是我觉得那是不好的方法。 As there can only be one user in the application - there should be a way of defining globally visible methods like current_user , user_signed_in() or sign_out(current_user) without calling JFrame. 由于应用程序中只能有一个用户-应该有一种方法来定义全局可见的方法,例如current_useruser_signed_in()sign_out(current_user)而不调用JFrame。 Is my feeling correct? 我的感觉正确吗?

Problem 2 问题2

During initialization of GUI I am creating a JTable that is displaying data downloaded from web with custom WebRequest class. 在GUI初始化期间,我正在创建一个JTable ,该JTable用来显示使用自定义WebRequest类从Web下载的数据。 JTable data is kept in Object data[][] a property of table model extending AbstractTableModel . JTable数据保存在Object data[][]这是扩展AbstractTableModel的表模型的属性。 Right now I am building the data model when the application starts - meaning calling a WebRequest, initializing Table and it's model. 现在,当应用程序启动时,我正在构建数据模型-这意味着调用WebRequest,初始化Table及其模型。

Right now I would like to change this behavior. 现在,我想更改此行为。 I would like to display empty JTable when the application starts - and call the WebRequest only by clicking JMenuItem refresh button. 我想在应用程序启动时显示空的JTable-并仅通过单击JMenuItem刷新按钮来调用WebRequest。

How to approach it? 如何处理呢? At the moment my only idea is to have JTable data model and call a one of it's methods - something like refresh() . 目前,我唯一的想法是拥有JTable数据模型并调用其中的一种方法-类似于refresh() This method should call WebRequest and update JTable . 该方法应调用WebRequest并更新JTable But for this I have to get from JMenuItem to JTable that is in completely different place. 但是为此,我必须从JMenuItem转到完全不同的地方的JTable

What can be better approach here? 有什么更好的方法? Again - there can be only one Data model to call in the app - so maybe a similliar problem to Problem 1? 再说一次-应用程序中只能调用一个数据模型-那么问题1可能有类似的问题吗?


Also if you know a good reading on design patters, or good open source repos to study - I would really appreciate any suggestions. 另外,如果您对设计方法有很好的了解,或者对学习的开源软件有很好的了解,我将不胜感激。

As you are looking for some guidance, I would suggest you have a look at the Oracle tutorial on Swing and give special attention to the Table and Model use parts of it as it will answer your second problem. 当您在寻找一些指导时,建议您看一下有关SwingOracle教程,并特别注意其中的TableModel使用部分,因为它可以回答您的第二个问题。

As for the first one, I would suggest you don't actually put your current_user element in the JFrame. 至于第一个,我建议您实际上不要将current_user元素放在 JFrame中。

The Model-View-Controler or Model-View-Presenter are good starting point as design-patterns here. Model-View-Controler或Model-View-Presenter是此处设计模式的良好起点。

For your first problem I would suggest an extre class named "Authentication". 对于第一个问题,我建议一个名为“ Authentication”的极端类。 This class should have a User. 此类应具有一个用户。 The class can also have following methods: signOut(), signIn(String username, String password),... 该类还可以具有以下方法:signOut(),signIn(String用户名,String密码),...

For the check I should do the following: 对于检查,我应该执行以下操作:

public boolean isLoggedIn(){
    if(currentUser != null){
        return true;
    }else{
        return false;
    }
}

IMPORTANT Java uses a naming convention: use mixedCase, no underscores. 重要事项 Java使用命名约定:使用mixedCase,不带下划线。

For the second problem: 对于第二个问题:

I would suggest to search for the Observer pattern for your refresh idea. 我建议搜索“观察者”模式以获取刷新提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM