aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Observer pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
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: 18650
    
    8

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Observer pattern