observer pattern question #2 (notification chain)
Alan Isaac
aisaac at american.edu
Thu May 8 10:50:17 EDT 2008
I have two questions about the "observer pattern" in Python.
This is question #2. (I'll put the other is a separate post.)
Consider this standard example of the observer pattern in Python:
<URL:http://en.wikipedia.org/wiki/Observer_pattern>
Now suppose I have a variant.
(I am not a programmer, so this may be a separate related pattern.)
To make it a little concrete and relate it to the discussion at
<URL:http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1>,
suppose there are stocks, mutual funds, and investors.
Let us say that funds are observers for stocks,
and investors are observers for funds.
Once a "month" I want all stocks to notify their observing funds
of their end-of-month price, and *then* all fund to notify their
observing investors of their end-of-month investment value.
I see a couple differences from the standard pattern referenced above,
and one problem.
Differences:
- observer/subject linked hierarchy (funds are both observers and subjects)
- many-many relation (a fund is an observer of many stocks)
I think (?) these differences are inessential, but the
second does create the following problem: if I do not want
a fund to notify an its investors until all stocks have
reported, what is the best way to determine the "last report
in" event?
Here is one way: create a ``reportreceived`` dict that maps
stocks to True or False, and after each stock notification
change False to True and check ``all(reportreceived.values())``
to determine whether it is ok to notify investors. When it
is ok, then notify investors and reset all the
``reportreceived`` values.
This is meant to be a general description so please do not
focus on the "real world" aspects, which are only for
illustration.
Thank you,
Alan Isaac
More information about the Python-list
mailing list