IntelliJ Java IDE
The moose likes OO, Patterns, UML and Refactoring and the fly likes Observer pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Observer pattern" Watch "Observer pattern" New topic
Author

Observer pattern

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I have a checkers game.

Model contains the game state and the view naturally displays the game.
The view is contained in a JFrame and this contains a few JPanels.

The model extends Observable and one of the views JPanels implements Observer.

When creating the model instance to hold the game state I need to run addObserver(JPanel instence) for the view to be notified when the model is updated.
Should I make the JPanel instance a singleton?

Kind of confused with so many references floating around.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

If you need only one of those Observer panels in your application, then just create one of them. There's usually no need to go through all that tedious Singleton business when a simple "Observer whatever = new ObserverPanel()" will do.
 
 
subject: Observer pattern
 
Threads others viewed
Separating View-Controller in MVC
MVC and Observer Pattern
Adding an action listener to an entire class
MVC
problem in refreshing JPanels in cardlayout
developer file tools