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?
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?
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?