473,721 Members | 4,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1478
Alan Isaac <ai****@america n.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****@america n.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.or g/wiki/Observer_patter n>

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****@america n.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...@america n.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
7040
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
1377
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
1932
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. etc). This is represented, for example, by a company window containing additional windows such as company information, document history, yada yada. I am using the Observer/Observable pattern to refresh all the forms in a group when another form...
1
6453
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 MVC-ipmlementation from the JAVA by using the observer-pattern. I used an interface IObservable (AddObserver, RemoveObserver,...). My Model implemented this interface.
0
1619
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 Observer Pattern, a Subject object notifies an Observer object if it's the Subject object's state changes. Pretty simple and fairly useful.
22
4726
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. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via Visitor Design Pattern. 3) Design Patterns Explained by Alan Shalloway and James Trott
0
1419
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 the solution please help me. #include<iostream> #include<list>
1
1873
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 general purpose type of Event. I want to write my design in such a way that more Event Types can be created and used down the road as needed. How can you design the Events in such a way that more can be handled later by the same Observer and Subject...
5
1231
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
8736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9143
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8019
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3202
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2137
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.