Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am working on refactoring an old simulation of a Turing machine.

The application uses a class that contains the state and the logic of program execution, and several panels to display the tape representation and show the state, messages, and the GUI controls (start, stop, program listing, ...).

I would like to refactor it using the MVC architecture that was not used originaly: the Frame is the only way to get access to the different panels and there is also a strong coupling between the "engine" class and the GUI updates in the way of frame.displayPanel.state.setText("halted"); or frame.outputPanel.messages.append("some thing");

It looks to me that I should put the state related code into an observable model class and make the different panels observers.

My problem is that the java Observable class only provides a global notification to the Observers, while I would prefer not to refresh every Observers everytime, but only when the part that specificaly observe has changed.

  • I am thinking of implementing myself several vectors of listeners (for the state / position, for the output messages, ...) but I feel like reinventing the wheel.
  • I though also about adding some flags that the observers could check like isNewMessageAvailable(), hasTapeMoved(), etc but it sounds also approximative design.

BTW, is it ok to keep the fetch / execute loop into the model or should I move it in another place? We can think in a theorical ideal way as I am completely revamping this small application.

share|improve this question
    
How about using the concept of firing events? Do a quick search about this concept. –  OSryx Nov 30 '12 at 14:36
    
@ALJIMohamed: yes, I was also thinking about defining several interfaces for triggering specific events to refresh seperately the different visual components. –  Seki Dec 3 '12 at 9:53

2 Answers 2

the java Observable/Observer classes allow you to add an object to get passed to the Observers' update method. You can then use that to differentiate what has changed (no generic version unfortunately)

share|improve this answer
    
So the regular way is for the Observer to use instanceof to check what is the given object that changed? Or to use a String that could tell the kind of notification? –  Seki Nov 30 '12 at 12:35
up vote 0 down vote accepted

I chose as suggested by @ALJIMohamed to address the different views via different lists of listeners and I fire different events to the view components that were registered by the controller.

It needs to define several EventObject and EventListener interfaces but I feel it better than making all the views simple observers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.