Re: Observer pattern limitations




"Gabriel Claramunt" wrote:

<snip>

If you want to discuss oberver pattern limitations you are welcome,
but let's discuss it in the scope of the pattern.
Observer pattern clearly shows the subject and a list of observers,
you have problems with a composition of subjects and observers in a
graph with cycles... IMHO that should be another pattern (Composition
of observers?), also no OO practitioner I know assembles multiples
patterns and expect to work just because they're patterns.
The dirty reads and cycles in the graph are valid problems but I fail
to see how they're in the scope of the observer pattern.
Maybe if you explain a little more about the problem, we can help to
find a better solution.

I pretty much agree, but just wanted to add this:

The Observer pattern has become the de facto way for objects to
communicate, at least with prefab objects that are often found in
libraries. For example, I use C#, and the .NET Framework is full of
components that have "events" for notifying interested parties when
something happens or changes with the components. Writing my own
libraries, I've often followed this pattern when creating components.
The Observer pattern is ubiquitous as it is built into the .NET
languages.

The promise of this architecture, and indeed the whole "component" idea,
is that you can approach programming in a "plug-and-play" fashion. Take
existing components, hook them together via events and event handlers,
and you have a lot of your work already done. Plus, there is the promise
of being able to reconfigure your application on the fly as you switch
out components.

But there are a whole host of issues that arise when you begin
programming this way. For example, in a multithreaded environment, you
need to know if an event notification is taking place on a different
thread than the one in which you wish to handle it. If so, you may need
to marshal said event to your thread. And what about exceptions? If you
run into an exceptional situation in an event handler, how do you deal
with it? If you throw an exception from the handler, where will it wind
up? It may pass through the subject who's in the middle of notifying its
observers. This could have all kinds of consequences.

My point is not that the Observer pattern is flawed but that it should
be used with care. The above problems have answers and can be solved,
but they do require that you first ask the right questions and then look
for suitable answers.

My participation in this thread, however, has been to explore
possibilities that will let us program from a higher level, so to speak.
Are there approaches that will let us more easily configure components
together without worrying about the issues that have been raised in this
thread? Can we find a generalized approach that can be used in a variety
of situations that will take care of these issues for us or make them
simply disappear? At the very least, is there a set of heuristics that
can be applied to the Observer pattern to make it more robust? Those are
the questions I'm interested in, and this thread has been interesting
along those lines.

Having said that, and this isn't directed towards you, Gabriel, I wish
to add that while I'm not the originator of this thread, I respect all
of those who have contributed to it and would be disappointed to see it
devolve into personal attacks. Let's give each other a benefit of a
doubt and take the high road.




.



Relevant Pages

  • Re: Observer pattern limitations
    ... The title of the thread was "Observer pattern ... The scope for which the observer pattern can be used successfully is ... is dangerous when there are chains of dependents. ... thread has to be held about limitations of a pattern where the ...
    (comp.object)
  • Re: Observer pattern limitations
    ... Observer pattern clearly shows the subject and a list of observers, ... to see how they're in the scope of the observer pattern. ... existing components, hook them together via events and event handlers, ... And what about exceptions? ...
    (comp.object)
  • Re: Observer pattern limitations
    ... but let's discuss it in the scope of the pattern. ... Observer pattern clearly shows the subject and a list of observers, ... And what about exceptions? ...
    (comp.object)