Is my program logic correct?

asked 2017-02-13 10:45:55 +0800

barracus gravatar image barracus flag of Italy
38 4

In my programs I use the next logic.

I create a MySession class with methods save, update, delete, loadAllRecordsFromMyTable, findMyRecord, etc, or rather every function that "query" database. In this class, I put also some static final consts like

protected static final Logger LOGGER = Logger.getLogger("Log");
protected static final List<EntityXYZ> allXYZ = new ArrayList<>();

EVERY viewmodal extends class MySession, but not every viewmodal uses all methods in MySession.

Here's my doubts:

  • I want to load some static list only once, cause they are immutable data in my database: is it correct to use a static final List<> in MySession? Where I have to load it the first (and unique) time?

  • Is this logic the correct one? Otherwise, which is the recommended approach?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted

answered 2017-02-13 13:26:32 +0800

chillworld gravatar image chillworld flag of Belgium
5143 3 9

updated 2017-02-14 09:18:44 +0800

About the logger, it's no problem to do that and if the name "Log" is good for every VM, yeah a static final would be the best approach.

Just take attention, a static final isn't for a Session but at application level.
So be carefull with naming the abstract VM.

About the list with entities....

This is for me not the right approach.
It looks like you want a kind of caching, so no extra queries are done to the DB.
caching happens at server side, not in your viewmodel, and there are some good strategies for it or you can make your own.
the only think you need to remember is that GUI is only graphical user interface and program logic isn't needed there except for visibility or disabling some buttons.
Caching stands under program logic (because you want a logic for speeding up the queries) and if you switch the GUI, you still want that, or not...?

hope this could help you.


Of course, getting the value from cache will be faster then query 3 times to the DB.
But be aware, a combobox need's a ListModel, if you provide a normal collection to the model, there will be autocasting in the back.
But, if you use a ListModel, never but never make that static, as selectedItems is hold in the ListModel also and you will have strange issues when you do.
Now, why I say that caching is needed in the server, think whenever you create a new item, or edit an already existing object, you can update your cache there with no problems.

your code should like this :

VM :

 private ListModelList<Car> carsModel = new ListModelList<>(carService.findAll());

CarService :

public Collection<Car> findAll() {
    return Collections.unmodifiableList (cache);

So, you see that I return an unmodifiable collection (in this case List).
It's just for not giving the UI code the opportunity to alter your collection, as this would be just dangerous.

A simple cache service could be :

public Collection<Car> findAll() {
    if (cache == null) {
        cache = carRepo.findAll();
    return Collections.unmodifiableList (cache);

public Car save (Car car) {
    if (findAll().contains(car)) { // to be sure that cache is filled we use the findAll()
    Car saved = carRepo.save(car);
    return saved;

With this you keep your cache always correct, except if other applications query your DB or manual interventions on the DB. (that's why I call it simple cache)
That's also the reason why you should do it in the server, just to intercept your save or delete or anything what could alter your cache.
Maybe this is not yet the case, but always be prepared for when the time is there.
A GUI doesn't need to know if your collection is cache or it comes from a DB or even a file, it only need's the collection from your server.


link publish delete flag offensive edit


Again, thank you a lot. Here http://stackoverflow.com/a/42224305/2550751 the answer to stackoverflow same question.

barracus ( 2017-02-14 10:56:29 +0800 )edit

answered 2017-02-13 16:31:08 +0800

barracus gravatar image barracus flag of Italy
38 4

Ciao Chill and thank you for the answer.

You're right, I did it for caching.

My purpose is to reduce the numbers of db query, as you mention.

In your opinion, in a view (designed to edit an Entity) with 3 comobobox fields (ex: vehiclemodel, year, ... all values stored in db tables), is it correct to query 3 times from db same fixed data every time I open that view? Does it impact the performance?

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2017-02-13 10:45:55 +0800

Seen: 35 times

Last updated: Feb 14