This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Observer pattern" Watch "Observer pattern" New topic

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

Joined: Oct 14, 2005
Posts: 19973

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.
I agree. Here's the link:
subject: Observer pattern
It's not a secret anymore!