observer pattern (notification chain synchronization)

Alan Isaac aisaac at american.edu
Fri May 9 09:51:21 EDT 2008


A question related to the observer pattern...

Suppose I have a variant: there are stocks, mutual funds, and investors.  Let us say that funds are observers for multiple stocks, and investors are observers for funds.  Once a "month" all stocks 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. 

What is a good way to enforce this timing?  (I.e., a fund should not notify an its investors until all stocks have reported.)

Here is one way:

- for each fund, create a ``reportreceived`` dict that maps stocks to booleans (initially False)
- as each stock notifies its funds, the fund changes False to True and checks ``all(reportreceived.values())`` to determine whether it is ok to notify investors.
- When it is ok, the fund notifies investors and resets all the ``reportreceived`` values. 

Is this sensible enough? What are standard and better ways?

Thank you, 
Alan Isaac 

PS I am drawing on the description of the observer pattern at
<URL:http://www.dofactory.com/Patterns/PatternObserver.aspx#_self1>
The real world aspects are just to add some concreteness.



More information about the Python-list mailing list