File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC + Observer pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC + Observer pattern" Watch "MVC + Observer pattern" New topic
Author

MVC + Observer pattern

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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()...


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
aadhi agathi
Ranch Hand

Joined: Apr 29, 2002
Posts: 263
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 ]

Aadhi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I gave some code for a MVC command-line calculator in this thread. Take a look and see if it helps.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Gopi Balaji
Ranch Hand

Joined: Jan 23, 2003
Posts: 84
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 ]
 
 
subject: MVC + Observer pattern