• Post Reply Bookmark Topic Watch Topic
  • New Topic

MVC + Observer  RSS feed

 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having big problems understanding how to construct a program using MVC using a Observer. If I for example have a gui which only contains a textfield and two buttons (save/load).
If I enter something to the field and press save the saved value should be saved to a file, and if I press load the value should be restored to the textfield. What I would think I must do is to create the following classes:

view: containing only the gui-part and an instance of the controller.
controller: containing a method for checking that the value is of correct type (like int).
model: only contains a instance variable and a get/set -method for that variable.
filewriter: has two methods that writes/reads to a file.

As I understand it I should use a actionListener for the save button that sends the value of the textfield to the controller to check if the value is valid. The controller should then use the set method of the model to set the value. From what I tried to learn the view should implement Observer, and model should extend Observable. But do I need more classes/interfaces and how exactly should I use the methods from Observer/Observable? I would like to be able to also extend the gui to be able to use five or ten textfields with the same functionality. So if three random textfields are containing text and the gui is cleared/restarted, when I press the load button all fields would be restored.

Have I hot anything of this right?
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of that does look right. Who said you have to use Observer/Observable?
 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must use it for a school-project but I can't really understand how to use a obeserver so I want to see how to use it in a really simple application.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CodeRanch FAQ. Look at the 8th bullet down.
 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really can't figure it out even after reading the faq.

I have this:









What changes do I need to make to get my code to work?
 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody that can answer my question or give me any link to a really basic Observer example?
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shou‍ld no longe use Observer. I have only known that for about ½ minute.
 
Knute Snortum
Sheriff
Posts: 4210
124
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Googling java 8 observer example finds several possibilities.  But this is one of those unfortunate situations where you have to do it the wrong not recommended way just for school.  You don't really need an Observer for MVC.
 
Paul Clapham
Sheriff
Posts: 22719
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't an ActionListener already an observer?

Or do you have to use a class or interface specifically named "Observer", or do you have to write your own observer code? All you have posted is your interpretation of your project, so maybe you're looking for unnecessary wheels to re-invent?
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An action listener listens to changes from its button or other component, whereas an observer listens for changes in its target. Any requests come from the opposite direction. I think the Wikipedia page is easy enough to understand.
 
Paul Clapham
Sheriff
Posts: 22719
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wikipedia wrote:The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.


This is an accurate description of how a Swing button works. It maintains a list of its action listeners and calls one of their methods when a particular action happens. In other words an ActionListener is an observer. I'm not sure what this opposite direction thing is that you're talking about -- maybe Wikipedia's idea of calling an object a subject had something to do with it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!