wood burning stoves 2.0*
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

venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130
Hi ,
I have a situation and have to use Observer pattern, In my case the Observable (subject) is a singleton, Now I have problem to addObserver(s) in constructor, since the constructor is static and addObserver method (I am reffering to method of java.util.Observable) is non static.
I actually want to add the observer when the instance is created. Is there any other way to do this.
Any suggestion will be greatly appreciated.
Thanks
Venkatesh


SCJP 1.4, SCWCD<p>Ours is a world where people don't know what they want and are willing to go through hell to get it.<br /> - Don Marquis
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
A singleton is still an object. I don't quite see what do you mean by not being able to addObserver() ?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
A constructor cannot be static, so must probably be speaking about something else. Perhaps you can show us some of the code you do have problems with?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130
Hi,
Well by constructor I meant the method(static) used to give a unique instance. Actually I was able to solve it, I had to put addObserver in constructor instead in create unique instance of singleton...
Code is as below:
public class StockMediator extends Observable implements Observer {
private static StockMediator _instance;
private StockMediator() {
addObserver(new StockBroker());
addObserver(new StockMonitor());
}
public static synchronized StockMediator getInstance() {
if (_instance == null) {
_instance = new StockMediator();
}
return _instance;}
....
Thanks
Venkatesh
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
For decoupling the observed would be better off not knowing about particular observers. You might have to introduce another class to coordinate everything:

Or since you mentioned the observed would be all static:

Now with those add calls coming from outside you can introduce new observers without touching the code in observed.
As an aside, the "if null create instance" technique you showed for making a singleton is dangerous in multi-threaded apps especially on multi-processor servers. It's safer and simpler to just create it when you declare it:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
As an aside, the "if null create instance" technique you showed for making a singleton is dangerous in multi-threaded apps especially on multi-processor servers.

Actually, it's quite safe the way he did it - synchronizing the whole method. What is broken is Double Checked Locking pattern, where you have to checks for null and only synchronize the second one.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, I missed the synchronized.
venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130
Hi ,
Thanks Stan for suggesting the decoupling by another class...I was thinking about that.
Thanks Ilja about Synchronized...
Thanks
Venkatesh
 
GeeCON Prague 2014
 
subject: Observer Pattern