Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC + Observer pattern

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please direct me to a java example of the MVC pattern which
- uses the Observer pattern without using any of the built-in java Observer pattern classes
- is text-based, not GUI-based
I have looked quite a bit on the web for some examples, and have not located any. It seems that many are preoccupied with MVC's use only in GUI or web applications, although MVC's use far extends these two areas. Your direction is much appreciated.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember seeing an example of MVC/Observer in a command-line program... Probably because the "dynamic" update of the user interface is a degree harder than with GUIs. You would need some kind of ncurses type-of library for drawing a character-based (G)UI.
An example for why the dynamic updates are a big leap from the good old, straight-forward, command prompt style user interface could be the user input itself. When the screen is updated, you cannot lose the stuff the user has typed in so far. I.e. you need to store the input somewhere instead of just calling myBufferedReader.readLine()...
 
aadhi agathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Erik Gfesser:
Please direct me to a java example of the MVC pattern which uses the Observer pattern without using any of the built-in java Observer pattern classes
I have looked quite a bit on the web for some examples, and have not located any. It seems that many are preoccupied with MVC's use only in GUI or web applications, although MVC's use far extends these two areas. Your direction is much appreciated.

please search for MVC in the developers forum.
the Observable will have have addListener, removeListener,notifyListener all taking a Listener and the Listener will be having an

1.i can either "pull" the data from the observable(update method above) in the observer
2."push" the data to the observers from observer.
Meaning either i will use the observable/data parameters in the udpate metjod. Using both will be unusual.
please let me know if it is useful
[ May 08, 2003: Message edited by: aadhi agathi ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took me a while to think of MVC or observer outside of AWT and Swing, but then I realized I've done it once or twice. I have a little utility that starts several threads, each of which fetches log files from servers. Each reports its status back to an observer, which writes a summary to the console. Does that give you any ideas?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave some code for a MVC command-line calculator in this thread. Take a look and see if it helps.
 
Gopi Balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Observer pattern as described by the poster - aadhi agathi is a generalized and simpler form of another pattern known as the Event-Subscription pattern. This is also used in AWT-Swing (actionPerformed(), windowClosed() ..).
I have used my own implementations of the Event-Notification pattern. (I am not much of a GUI programmer).
In one case : I read three sets of huge number of CSV files. As each line is read, I publish an instance of an FileReadEvent which contains usefull information of the event. The subclass of the Event will indicate which type of CSV file was read, and the event object will contain the name of the file, line number and line just read.
There will be several "listener"s who will subscribe to the CSV file reader, but will subscribe to only specific events (Type1FileRead). As they receive "notification"s of the events, they perform certain actions.
In another case : In an implementation of the Route-Vector algorithm to simulate a network akin to a TCP/IP network, I used the Event-Subscription pattern. As the route costs were changed, events were generated, which were listened to, by all interested nodes, which performed routing table calculations, based on this event. Other kinds of events were nodes going offline, routes disappearing, traffic-beyond-threshold.
This is a wonderful pattern, and can be used well in many cases..
The main difference between stock Observer pattern, and the (more sophisticated) Event-Subscription pattern is that, in the latter, different kinds of Events can be defined, and you can choose to subscribe and listen to only such events.
-GB.
[ May 15, 2003: Message edited by: Gopi Balaji ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic