A
antoine
Hello,
after having developped several graphical applications "that just work"
(dirty code), I've decided to clean the code a bit, especially to
separate business logic from graphic elements.
one thing I've started to use heavily is "some kind" of observer /
listener pattern to link data displayed in components to data hold in
logical structures.
for that, I actually don't really understand the difference between the
Observers and Listeners. to me it appears that Events listened for by
Listeners are fired on the Event Dispatching Thread, while using
Observers implies I stay on the same thread. first, am I correct to say
such thing ?
on my end, I've implemented a mix of both methods, by creating
interfaces that I could call "myObjectListener", that will define only
one method: update(int source), and several public constants int
describing the possible sources.
I also define a notifyListeners(int source) and a
addListener(myObjectListener l) in my BIG "business" element. listeners
hold in a vector, and I use Iterator... (even though I have only ONE
listener most of the time).
in doing so, when element A changes in my BIG "business" element, I
simply call a notifyListeners(source_A), that does a callback on all my
listeners, calling for the "graphic" update of element A ONLY.
question is simple: is that clean code ? what's wrong with all that ?
I mean, it's working OK, I might be paranoid, but I'm pretty sure
there's an argument against this method, so that's why I'm asking the
experts of this group
wouldn't there be a way to do this that would make more sense ? how can
this compare to the Listeners / Observers patterns ?
thanks for your feedback, hope I've been clear enough...
-Antoine
after having developped several graphical applications "that just work"
(dirty code), I've decided to clean the code a bit, especially to
separate business logic from graphic elements.
one thing I've started to use heavily is "some kind" of observer /
listener pattern to link data displayed in components to data hold in
logical structures.
for that, I actually don't really understand the difference between the
Observers and Listeners. to me it appears that Events listened for by
Listeners are fired on the Event Dispatching Thread, while using
Observers implies I stay on the same thread. first, am I correct to say
such thing ?
on my end, I've implemented a mix of both methods, by creating
interfaces that I could call "myObjectListener", that will define only
one method: update(int source), and several public constants int
describing the possible sources.
I also define a notifyListeners(int source) and a
addListener(myObjectListener l) in my BIG "business" element. listeners
hold in a vector, and I use Iterator... (even though I have only ONE
listener most of the time).
in doing so, when element A changes in my BIG "business" element, I
simply call a notifyListeners(source_A), that does a callback on all my
listeners, calling for the "graphic" update of element A ONLY.
question is simple: is that clean code ? what's wrong with all that ?
I mean, it's working OK, I might be paranoid, but I'm pretty sure
there's an argument against this method, so that's why I'm asking the
experts of this group
wouldn't there be a way to do this that would make more sense ? how can
this compare to the Listeners / Observers patterns ?
thanks for your feedback, hope I've been clear enough...
-Antoine