NaC114
NaC114

Reputation: 83

Best way to add a Class to serve as a database in Java

I'm writing a simple Java program to serve as a company data base. I.e. ability to add customer, products etc.

I'm using JSON to store data and I have a class DataBase, which as the name implies is where I store all the data.

I'm working on the GUI now with swing, and I have multiple windows, panes etc. Since the GUI is "big" I decided to have the construction of certain JPanel s in different classes most of which need to get and set objects from DataBase class.

The problem though is that, if I do it this way I have to make DataBase public. Any advice on this, am I doing it right (which I doubt) or?

Thank you for your advice.

Upvotes: 1

Views: 43

Answers (1)

npinti
npinti

Reputation: 52185

Proper structure would in my opinion, still require you make the DataBase class public.

Ideally, you structure your application in such a way that the layers are loosely coupled but demonstrate high cohesion, meaning that the layers should not be intertwined together but be able to work well together.

This would mean that your data manipulation layer would not be aware of the GUI layer, and in turn, your GUI layer would not be aware of a data layer, but rather both should provide functionalities to manipulate data and display it.

Such structuring would require the setting up of seperate packages, which should in turn expose the logical structure of your application.

As a side note, naming a class DataBase which has nothing to do with database can be misleading. Something like DataStore would still mean that it is the class which handles data, but it does not imply what methodology is being used. Another minor note would be that to perform the usual CRUD operations and search operations, you would either need to load everything in memory which can be resource intensive or else, read from file one item at a time, which can be slow. If this is not an assignment, going with an actual database would be the way to go.

Upvotes: 1

Related Questions