Question

I am creating a 3 tier application in java which consists of a gui, a business logic layer and a database layer. All layers are client-sided.

From the definition of a multi tier architecture it is only allowed to make calls to the same or lower layers and return to the same or higher layers.

In my case I'm doing a lot of database queries after the user clicked on a button. I would like to have a status field in my gui where the currently queried table is shown.

Because all layers are in the same application, I could just call a method from the database layer while looping through the tables to update the status field. But doing so would break the rule to only make calls to lower or the same layer.

So what would be the "legit" way to update or notify a gui from lower layers in a 3 tier architecture?

Was it helpful?

Solution

I suggest to use an observer pattern. By using java.util.Observable class (as subject) and the java.util.Observer interface you will still keep the conventions.

  1. You create a concrete observer by subclassing java.util.Observer (GUI- Layer)
  2. You create a concreate Subject by subclassing java.util.Observable (DAO- Layer)
  3. You can attach your concrete observer to the concreate subject. (Referencing lower layers doesn't violate the convention!)
  4. A button click invokes the desired method (Business- or DAO- Layer) by delegating the concreate observer instance as type of java.util.Observer. So the DAO- Layer will never need to reference the GUI- Layer.

So the GUI calls a method like:

BusinessImpl#doDAOStuff(java.util.Observer observer){
    ...
    dao.performStatements(observer);
    ....
}

And the DAO impl should look like:

DAOImpl#performStatements(java.util.Observer observer){
    String stmt;
    ...

    // do insert ...
    observer.update(this, stmt);

    ...
    // do update ...
    observer.update(this, stmt);

    ...
}

It's just pseudo source, but I think it covers the main concept.

OTHER TIPS

Ideally you should use the middle layer to communicate from the db layer to ui layer as recommended by MVC. But if you really have a need to do as you desire then how about implementing the Observer pattern at each layer. Make DB layer Observable, add the UI layer as and observer to it. Whenever you want to update the UI, call the related observer and send the information which can be consumed and displayed by UI.

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