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 5 1211
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.
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
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
Alan Isaac <ai****@american.eduwrites:
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.
Alan Isaac <ai****@american.eduwrites:
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 ;-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andy Read |
last post by:
Hello all,
I have the requirement to produce source code that produces an object
hierarchy.
Example:
Root
|
Folder 1
|
by: Paolino |
last post by:
Lately I was needing to use multiple inheritance to split behaviour of a
class and modularize it.
But the problem raises when the need is to add operations to a method
already present in one of...
|
by: Michael Schneider |
last post by:
Hello All,
I am comming back to python after being away for several years.
I would like to use weak refs in an observer pattern implementation.
The problme that I have seems to be that...
|
by: decrypted |
last post by:
Since I couldn't find a OO design/architext forum, I thought I would post
here...
I have a win app with forms management. forms are grouped by category
(pertains to company, pertains to project....
|
by: FluffyCat |
last post by:
Last week I continued my series of design patterns examples using PHP
5. Here now is my 14th example, the Observer pattern.
http://www.fluffycat.com/PHP-Design-Patterns-Observer/
In the...
|
by: Krivenok Dmitry |
last post by:
Hello All!
I am trying to implement my own Design Patterns Library.
I have read the following documentation about Observer Pattern:
1) Design Patterns by GoF
Classic description of Observer....
|
by: Gianni Mariani |
last post by:
Two issues here:
a) What is the accepted definition of "observer pattern". While I can't
point to anything specific, I do remember having issues with
inconsistency in the definition.
b)...
|
by: Mohamed Mansour |
last post by:
Hello,
What is the purpose of implementing the Observer Pattern if we can trigger
an event easily?
For example (from books),
You have a "Forecaster" which notifies "Observable" when a...
|
by: Alan Isaac |
last post by:
I have two questions about the "observer pattern" in Python.
This is question #1. (I'll put the other is a separate post.)
Here is a standard example of the observer pattern in Python:
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |