Skip to main content
1 of 3
rolfl
  • 97.5k
  • 17
  • 218
  • 418

A Generic Observable is a neat idea. Much of the code that was initially designed for the Obeserver pattern in Java was then actually built using a custom interface for each 'event', and you ended up, for example, with the Listener and Event interfaces that are so common in the AWT/Swing frameworks. A more general-purpose system has merits though.

What will be the usage pattern for it, though? Is there ever a need to create subclasses? Is there a need for the factory-methods you have created? Note, those factory methods cannot ever create instances of a sub-class.

Further, you have synchronizaton built in to it, but the synchronization is on the instance, which is unsafe (what if someone synchronized on your instance for some other reason, suddenly your locking strategy is confounded).

Finally, there's a reason that the Observer instance in the JDK has a two-argument notify, and that's because it needs to know where the notification came from. That way the Observer can observe multiple Observables, and know which one the notification originates from.

These are 4 significant issues I see in your design.

Your implementation is a bit shaky too:

  • the indentation is a mess
  • braced 1-liners
  • poor variable names
  • duplicate methods (deleteObserver calls rm, addObserver calls add, deleteObservers calls clear)
  • unimplemented methods (getInstance())

All in all, there's a lot to consider, and this is not done the way I would expect.

rolfl
  • 97.5k
  • 17
  • 218
  • 418