Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Observer Pattern

 
venkatesh rajmendram
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A singleton is still an object. I don't quite see what do you mean by not being able to addObserver() ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
venkatesh rajmendram
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I missed the synchronized.
 
venkatesh rajmendram
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Thanks Stan for suggesting the decoupling by another class...I was thinking about that.
Thanks Ilja about Synchronized...
Thanks
Venkatesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic