user3756824
user3756824

Reputation: 107

Swing and observer pattern

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.

Upvotes: 3

Views: 3368

Answers (1)

trashgod
trashgod

Reputation: 205785

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.

Upvotes: 8

Related Questions