3

I had a task at university of making an application using observer pattern to pass the subject (basics of programming). I wanted to show more knowledge, that it is included in syllabus and it backfired on me. I made an application in Swing + sql, heavily dependable on ActionListeners, which I thought, are a significant example of observer pattern. Unfortunately, my project was rejected because I did not write my own implementation of Observer pattern.

My question is, is that even possible with Swing to replace default action listener? I cannot even check a state of a button outside of it. I am really confused at the moment, even though I know the principles of a pattern and looking for help from your side.

user3756824
  • 109
  • 2
  • 9
  • The usage of an `ActionListener` to begin with is part of the `Observer` pattern implementation. http://stackoverflow.com/a/3359101/2413303 – EpicPandaForce Jan 26 '15 at 14:38
  • @EpicPandaForce So what in that case would be considered as a full implementation? – user3756824 Jan 26 '15 at 14:42
  • what were the specifications? Did they say "make a Swing application", or just a Java application? Swing already provides an implementation for the Observer pattern, after all. – EpicPandaForce Jan 26 '15 at 15:19
  • @EpicPandaForce Java application freely, no limitations, so I assume Swing was allowed. I just needed the Observer pattern. But then it turns out, it must be an OWN implementation, so I am really lost. – user3756824 Jan 26 '15 at 16:27

1 Answers1

8

Three common ways to implement the observer pattern in Swing are described here. The simplest to emulate would be Observer, a single-method interface, and Observable, a class that holds (in effect) a List<Observer>. Invoking notifyObservers() traverses the List, calling the update() method of each Observer in the list.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045