473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

observer pattern question #1 (reference to subject)

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:

http://en.wikipedia.org/wiki/Observer_pattern

Contrast with this rather standard discussion:

http://www.dofactory.com/Patterns/Pa...er.aspx#_self1

The difference I am focusing on is that in the latter,

the observer (investor) maintains a reference to the

subject (stock).

(Many questions can be raised of course: see the discussion at

http://aspn.activestate.com/ASPN/Coo...Recipe/131499).

Is anything lost by not maintaining this reference (other

than error checking ...)? If I feel the observer needs

access to the subject, what is wrong with just having the

subject pass itself as part of the notification?

Thank you,

Alan Isaac
Jun 27 '08 #1
5 1456
Alan Isaac <ai****@american.eduwrites:

Is anything lost by not maintaining this reference (other

than error checking ...)? If I feel the observer needs

access to the subject, what is wrong with just having the

subject pass itself as part of the notification?
It reduces the number of places the observer can be called from,
because now the event source is part of the function signature. If you
omit the event source in the signature, you gain looser coupling -
it's the observer business to create the dependency.

Also, consider the situation where you want all investors to refresh
their idea about stock prices (because of, short network
failure). It's easy to run through a list of them and call update()
for everybody, while it's not so easy to find out what stock the
investor is observing (that's the investors business!) and pass that
object to the investor in the call.

Are these school questions btw? ;-)
Jun 27 '08 #2
Alan Isaac <ai****@american.eduwrites:
>Is anything lost by not maintaining this reference (other
>than error checking ...)? If I feel the observer needs
>access to the subject, what is wrong with just having the
>subject pass itself as part of the notification?


Ville M. Vainio wrote:
It reduces the number of places the observer can be called
from, because now the event source is part of the function
signature. If you omit the event source in the signature,
you gain looser coupling - it's the observer business to
create the dependency.


As I said, I'm not a programmer, and perhaps as a result,

I have questions about both of your points. I'll stick with

the stock example, since it is concrete.

Note that the observor/listener in the example at

<URL:http://en.wikipedia.org/wiki/Observer_pattern>

does not create a reference to the subject.

So in this context I take you to be saying something like

the following: "OK, here's the pattern, now your listener

wants to know the event source, do not ask something new the

subject to respond to that need. That is unnecessary

coupling. Instead, just rewrite your listener to maintain

a reference to the subject."

Do I have that right?

It seems to me that this response begs the question.

That is, an appropriate reply seems to be: here is a related

pattern, call it observer2. In the observer2 pattern, the

subject has a getState method and always passes itself when

it notifies the observer. Now: in what sense does this

require closer coupling?


Also, consider the situation where you want all investors
to refresh their idea about stock prices (because of,
short network failure). It's easy to run through a list of
them and call update() for everybody, while it's not so
easy to find out what stock the investor is observing
(that's the investors business!) and pass that object to
the investor in the call.


I seem to be missing your point here.

Let's keep using the stock/investor example.

When the network comes up, each stock should notifies

everyone in its list of listeners. It either passes itself

or does not. In either case, the investor took care of hir

side of the business by registering as a listener.

If the stock does not notify all its registered investors,

it is breaking its contract. So, what is the problem?

Or to put it a different way, your suggestion that the

investors should have to *pull* this information from the

stocks seems out of step with the observer pattern, where

the stocks should be pushing this information to the

investor.

Again, talk of stocks and investors is just a convenient

short hand for discussing the observer pattern. I do not

care about stocks/investors more than other applications.
Are these school questions btw?


No, sorry. But I still appreciate the feedback.

Cheers,

Alan Isaac
Jun 27 '08 #3
Alan Isaac <ai****@american.eduwrites:
the following: "OK, here's the pattern, now your listener
wants to know the event source, do not ask something new the
subject to respond to that need. That is unnecessary
coupling. Instead, just rewrite your listener to maintain
a reference to the subject."

Do I have that right?
Not really. It has to be determined case-by-case. Note that I'm not
arguing *for* not passing "subject" on notification, just trying to
elaborate on situations that might want to choose that route.
It seems to me that this response begs the question.
That is, an appropriate reply seems to be: here is a related
pattern, call it observer2. In the observer2 pattern, the
subject has a getState method and always passes itself when
it notifies the observer. Now: in what sense does this
require closer coupling?
It creates stronger connection between the subject and observer,
thereby reducing the amount different of places that can call the
observer (I don't mean different stock, but altogether different
places - say, a central stock controller that can broadcast the change
in several stocks at the time by stock id, once per second).

This is not really a feature of the observer pattern, both approaches
are somewhat valid observer patterns, depending on problem domain. If
you think it makes sense in the first place to have ONE reference to
the subject in observer, it's probably a workable approach - but in
case of stocks, you are probably monitoring several stock objects, so
the stock should probably pass itself to the observer (so that you
don't need to check all stocks every time something changes!).

Of course this stock thing is a bad example for this case, because you
have several stocks per investor. It's analogous to a typical UI
scenario where you have several widgets forwarding events to one big
observer - and they *do* provide the event source and event type.
Or to put it a different way, your suggestion that the
investors should have to *pull* this information from the
stocks seems out of step with the observer pattern, where
the stocks should be pushing this information to the
investor.
It's not necessarily the observer that has the interesting
information. It's quite typical to have general "catch-all"
notifications than report observers that "something has
chanced somewhere, so you should refresh your data".

All in all, though, the right solution should be obvious from the
problem at hand.
Jun 27 '08 #4
Ville M. Vainio wrote:
in case of stocks, you are probably monitoring several
stock objects, so the stock should probably pass itself to
the observer


OK. This is related to my question #2 (in a separate

thread), where I'd also appreciate your comments.


analogous to a typical UI scenario where you have several
widgets forwarding events to one big observer - and they
*do* provide the event source and event type.


That is a helpful comparison.


All in all, though, the right solution should be obvious
from the problem at hand.


For CS types, maybe. The rest of us are just trying to get

things done without as much background and context.

Thanks,

Alan
Jun 27 '08 #5
On May 8, 4:57*pm, Alan Isaac <ais...@american.eduwrote:
Ville M. Vainio wrote:
in case of stocks, you are probably monitoring several
stock objects, so the stock should probably pass itself to
the observer

OK. *This is related to my question #2 (in a separate

thread), where I'd also appreciate your comments.
analogous to a typical UI scenario where you have several
widgets forwarding events to one big observer - and they
*do* provide the event source and event type.

That is a helpful comparison.
All in all, though, the right solution should be obvious
from the problem at hand.

For CS types, maybe. *The rest of us are just trying to get

things done without as much background and context.

Thanks,

Alan
My local vernacular calls them "carfulls" of people, which analogy to
the body of collective man, extends pretty far.

It's not the gas pedal we hate! I'm schedule more coasting on shore.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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
1
by: mem | last post by:
Class subject { private: List<Observer*> *_observers; .... }; Why use a pointer, _observers, above? Is it really necessary to have Subject and Observer base classes in the
4
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....
1
by: Marcel Hug | last post by:
Hi NG ! I have already written a task about MVC and I tried to get the best informations together. I would like to implement the MVC pattern and it work on the way I did it. At first i know the...
0
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...
22
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....
0
by: NiveditaB06 | last post by:
Can anyone please help me? I tried this problem , it is giving a runtime error in the below mentioned line. _observers->push_back(o); Anyone have any explanation for the reason & if anyone knows...
1
by: Christopher | last post by:
The observer pattern itself is easy enough. I've implemented it using a Event that contains data for any Event type I forsee my application using. My problem is I don't want one and only one...
5
by: Alan Isaac | last post by:
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:
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.