Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

observer pattern (notification chain synchronization)

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


aisaac at american

May 9, 2008, 6:51 AM

Post #1 of 5 (85 views)
Permalink
observer pattern (notification chain synchronization)

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.
--
http://mail.python.org/mailman/listinfo/python-list


jcd at sdf

May 9, 2008, 7:14 AM

Post #2 of 5 (78 views)
Permalink
Re: observer pattern (notification chain synchronization) [In reply to]

That looks like a good approach to me. Alternative to a dict would just
be a count of reported stocks tested against the total number of stocks,
but if one stock reports twice before another reports at all, you'll get
funny results. Your method is probably better, in the general case.

Cheers,
Cliff


On Fri, 2008-05-09 at 13:51 +0000, Alan Isaac wrote:
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
http://mail.python.org/mailman/listinfo/python-list


aisaac at american

May 10, 2008, 9:08 PM

Post #3 of 5 (67 views)
Permalink
Re: observer pattern (notification chain synchronization) [In reply to]

J. Cliff Dyer wrote:

> looks like a good approach to me



OK, thanks.

Another approach is to begin with a set of stocks

and remove them as they report. You can then trigger

a report with the empty set instead of repeatedly

calling ``all``. After a report the set can be

"refilled".



Cheers,

Alan Isaac


--
http://mail.python.org/mailman/listinfo/python-list


vivainio at gmail

May 11, 2008, 12:59 PM

Post #4 of 5 (66 views)
Permalink
Re: observer pattern (notification chain synchronization) [In reply to]

Alan Isaac <aisaac[at]american.edu> writes:


> 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?

You could explore the performance of popping items from the dict/set,
instead of toggling the value so to true. Once the dict is empty, you
are done. Of course the dict/set would be called
stocks_that_havent_reported, or a saner & shorter variant of that.

The idea here is that checking a dict/set for emptiness is
close-to-zero time operation.


--
http://mail.python.org/mailman/listinfo/python-list


vivainio at gmail

May 11, 2008, 1:00 PM

Post #5 of 5 (66 views)
Permalink
Re: observer pattern (notification chain synchronization) [In reply to]

Alan Isaac <aisaac[at]american.edu> writes:


> OK, thanks.
>
> Another approach is to begin with a set of stocks
>
> and remove them as they report. You can then trigger
>
> a report with the empty set instead of repeatedly
>
> calling ``all``. After a report the set can be
>
> "refilled".

Ah, and I obviously didn't read the whole thread before posting ;-)
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.