Architecture problem to handles messages

Hi,

I have a java application with a gui. My teachers learned that the logic classes shouldn't receive any gui classes in parameter..but i would like to write messages (informing on what's happens in my program)into a panel of my application. But i can't see how to do that without to give my panel to my logic class...

Exemple :

class MyBankAccount{

//do my logic

}

class MyMessagePanel extends JPanel{

//write message to the user

}

how to write into a myMessagePanel object from a myBankAccount object without giving the myMessagePanel object in parameter to the myAccountBank..

i'm pretty sure there is a solution that i haven't discover yet

Thanks for your help

Gaetan

[758 byte] By [gaetan06a] at [2007-9-28 1:45:40]
# 1

Hello,

Seperation of model, view and view is a old design pattern. It is not, strictly specaking an architectural style. You could find more information on the subject in ootips.org

I shall try to explain how you could use this in your application. You have two categories of component:

1) The View components (GUI)

2) Controller Components (Logic Classes)

The basic idea is to send messages from the GUI to the Logic Classes. It can be done in many ways. There is nothing wrong in giving the reference of one type of component to another i.e. GUI has reference to logic classes and the logic classes have reference to the GUI. However, I do agree that it is not in good form. What you could do is to implement two interfaces, one on the GUI and another on the Logic classes and pass an object implementing the interface to the GUI and the Logic classes. This would result in a lower coupling between the two types of component.

If you want even lower coupling, use "implicit Invocation". This is an architectural style that relies on asynchronous messaging. The idea is to send messages that is system wide and only the components interested in the message will use it to perform some action. It is not necessarily very simple but is powerful and flexible. Java has APIs that support this paradigm - the javax.infobus package of InfoBus API has the required classes.

However, since you are still learning, I would suggest implementing the interfaces and using "explicit invocation"

Your problem could be solved by many other ways.

Ironluca

Ironlucaa at 2007-7-7 21:18:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

hi,

In order to solve this problem i was thinking about to use the Observer/Obsvervable classes.

For this, i think that it could be a good solution to have a Message classes that could be observed by all classes of my application.

Then my messagePanel will observe this class and when the message will change, my panel which oberves this class could display it.

The schema of my classes will be like this

class MyBankAccount{

//send messages using the class MyMessage

//I think i'll have a static message object for all my application

}

class MyMessage extends Observable

//handles messages and notify observer when the message has changed

}

class MyMessagePanel extends JPanel implements Oserver{

//display message when it has been changed...

}

do you understand this chema?

do you think it a good alternative?

Otherwise, Ironluca, do you have a schema example for your solution using interfaces?

Note: My messages are just message of String type to inform the user by using the message panel what my logic classes are doing..instead of using the System.out....

Thanks

gaetan06a at 2007-7-7 21:18:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
Yes, the Observer is also a kind of implicit invocation. It can be used effectively and it is also easy to implement and supported by interfaces in core java. I think you should use it, the next bet is InfoBus but it is a bit more involving.Ironluca
Ironlucaa at 2007-7-7 21:18:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

It didn't seem clear to me in the above so just to make it obvious...

Your logic layer shouldn't be writing anything to the gui. So there is never a problem.

You might be addressing one of the following.

1. You are trying to display "debug" information. This is information that has nothing to do with the application but which you want to use to figure out what is happening. In commercial applications this is usually written to a file. For you usage (school) you could just write it to the screen using System.out.println().

2. You are trying to display information vital to the application. Then something is wrong with your logic layer. The logic layer does not do output. It just does rules. Now the GUI might need to display something that the logic layer as produced but the logic layer passes that information to the GUI layer. It does not do anything itself to make it show up on the screen. The previous posts suggested ways to implement that interface.

jschella at 2007-7-7 21:18:41 > top of Java-index,Other Topics,Patterns & OO Design...