Question

Recently I began an adventure with JAVA. I am looking for a guidance on design patterns and overall architecture of JAVA project.

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.

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

Login problem - I have custom User class. I keep the current_user as property of main 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.

Currently I solve both problems by going all the way to mainFrame.current_user property. 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. Is my feeling correct?

Problem 2

During initialization of GUI I am creating a JTable that is displaying data downloaded from web with custom WebRequest class. JTable data is kept in Object data[][] a property of table model extending AbstractTableModel. Right now I am building the data model when the application starts - meaning calling a WebRequest, initializing Table and it's model.

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.

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(). This method should call WebRequest and update JTable. But for this I have to get from JMenuItem to JTable that is in completely different place.

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?


Also if you know a good reading on design patters, or good open source repos to study - I would really appreciate any suggestions.

Was it helpful?

Solution

For your first problem I would suggest an extre class named "Authentication". This class should have a User. The class can also have following methods: signOut(), signIn(String username, String password),...

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.

For the second problem:

I would suggest to search for the Observer pattern for your refresh idea.

OTHER TIPS

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.

As for the first one, I would suggest you don't actually put your current_user element in the JFrame.

The Model-View-Controler or Model-View-Presenter are good starting point as design-patterns here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top