473,739 Members | 7,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Purpose of Implementing the Observer Pattern

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 prediction is
ready, then there is a "WeatherVie wer" which calls methods from the
"Observer Interface".

What is the point of implementing the Observer Pattern, we could have just
make and fire off an Custom Event stating "PredictionRead yEvent".

Can someone tell me what the point of using this Gang of For pattern in
..NET. Maybe I am just misinterpreting it.

Thanks
--
Regards,
Mohamed Mansour
Microsoft Student Partner

Dec 19 '07 #1
4 2115
The question is "who is driving"?

If you are setting up software where the item which would be observed is the
driver, then there is no reason to have the observer pattern. If, instead,
you are setting up a less "aware" object, then the observer works.

Okay, here is a shot. In OOP, you generally end up with two types of objects
on your business layer. The first are state objects, the other are behavior
objects. State objects hold state, or serve as "data" models. For example, a
customer object contains the name, address, phone, etc. of a customer.
Behavior objects do work.

While you do see some systems designed where the customer object is aware of
the data contracts, this is generally a mistake. It is generally better to
have another object fill, update, save, etc. (proxy the CRUD for the
object).

Following this generality, if you have a state object that you want to watch
changes in state, you can either add an event (add behavior to a state
object) or add a piece that watches the object (place behavior in a behavior
object).

Now, let's imagine our cutomer object and we want to know some type of
change in state. I can have the object load up and fire off an event. But,
if I do this, the object is handling both state and behavior. There is
nothing inherently wrong with this, but it does cloud intent, which will
generally make my solution less maintainable. My other option is to have
another object watch for the state change.

Probably not the best examples, but I am flying by the seat of my pants.
Hopefully, however, the idea of intent in code and the generality of
separating state and behavior will give you enough of a base to work with.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************** *************** *************** ****
| Think outside the box!
|
*************** *************** *************** ****
"Mohamed Mansour" <m0@community.n ospamwrote in message
news:5D******** *************** ***********@mic rosoft.com...
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 prediction is
ready, then there is a "WeatherVie wer" which calls methods from the
"Observer Interface".

What is the point of implementing the Observer Pattern, we could have just
make and fire off an Custom Event stating "PredictionRead yEvent".

Can someone tell me what the point of using this Gang of For pattern in
.NET. Maybe I am just misinterpreting it.

Thanks
--
Regards,
Mohamed Mansour
Microsoft Student Partner

Dec 19 '07 #2
Thank you Gregory :) Very well explained !

I can now see many situations where implementing the observable pattern
would better than firing off a simple event.

Thanks! Much appreciated!

--
Regards,
Mohamed Mansour
Microsoft Student Partner

"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMwrote in
message news:um******** ******@TK2MSFTN GP02.phx.gbl...
The question is "who is driving"?

If you are setting up software where the item which would be observed is
the driver, then there is no reason to have the observer pattern. If,
instead, you are setting up a less "aware" object, then the observer
works.

Okay, here is a shot. In OOP, you generally end up with two types of
objects on your business layer. The first are state objects, the other are
behavior objects. State objects hold state, or serve as "data" models. For
example, a customer object contains the name, address, phone, etc. of a
customer. Behavior objects do work.

While you do see some systems designed where the customer object is aware
of the data contracts, this is generally a mistake. It is generally better
to have another object fill, update, save, etc. (proxy the CRUD for the
object).

Following this generality, if you have a state object that you want to
watch changes in state, you can either add an event (add behavior to a
state object) or add a piece that watches the object (place behavior in a
behavior object).

Now, let's imagine our cutomer object and we want to know some type of
change in state. I can have the object load up and fire off an event. But,
if I do this, the object is handling both state and behavior. There is
nothing inherently wrong with this, but it does cloud intent, which will
generally make my solution less maintainable. My other option is to have
another object watch for the state change.

Probably not the best examples, but I am flying by the seat of my pants.
Hopefully, however, the idea of intent in code and the generality of
separating state and behavior will give you enough of a base to work with.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************** *************** *************** ****
| Think outside the box! |
*************** *************** *************** ****
"Mohamed Mansour" <m0@community.n ospamwrote in message
news:5D******** *************** ***********@mic rosoft.com...
>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 prediction is
ready, then there is a "WeatherVie wer" which calls methods from the
"Observer Interface".

What is the point of implementing the Observer Pattern, we could have
just make and fire off an Custom Event stating "PredictionRead yEvent".

Can someone tell me what the point of using this Gang of For pattern in
.NET. Maybe I am just misinterpreting it.

Thanks
--
Regards,
Mohamed Mansour
Microsoft Student Partner

Dec 19 '07 #3
"Mohamed Mansour" <m0@community.n ospamwrote in message
news:5D******** *************** ***********@mic rosoft.com...
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 prediction is
ready, then there is a "WeatherVie wer" which calls methods from the
"Observer Interface".

What is the point of implementing the Observer Pattern, we could have just
make and fire off an Custom Event stating "PredictionRead yEvent".

Can someone tell me what the point of using this Gang of For pattern in
.NET. Maybe I am just misinterpreting it.
I believe that events in .NET already implement most, if not all, of what
the GoF Observer pattern is for. They separate the object being observed
from the objects observing it.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer
Dec 19 '07 #4
I would say the same thing. And phrase it this way.

..Net framework kinda has a "built in" observer pattern setup...with events
raising.


"John Saunders [MVP]" <john.saunder s at trizetto.comwro te in message
news:u8******** ******@TK2MSFTN GP06.phx.gbl...
"Mohamed Mansour" <m0@community.n ospamwrote in message
news:5D******** *************** ***********@mic rosoft.com...
>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 prediction is
ready, then there is a "WeatherVie wer" which calls methods from the
"Observer Interface".

What is the point of implementing the Observer Pattern, we could have
just make and fire off an Custom Event stating "PredictionRead yEvent".

Can someone tell me what the point of using this Gang of For pattern in
.NET. Maybe I am just misinterpreting it.

I believe that events in .NET already implement most, if not all, of what
the GoF Observer pattern is for. They separate the object being observed
from the objects observing it.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer


Dec 27 '07 #5

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

Similar topics

0
7041
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
2
3598
by: cppaddict | last post by:
I have a design question which I am posting here because the implementation will be in C++ and I think there may be C++ specific language constructs (eg, friends) that might be relevant to the solution. I am implementing the Mediator pattern to create an event-driven app. The Collaborators (which watch for various events and which all have a reference to the Mediator object) will call handleEvent(Event e) on the Mediator object when...
3
2627
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 weakrefs can't manage functions. ------------------- from docs: http://www.python.org/doc/current/lib/module-weakref.html
4
1933
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...
0
1620
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
4732
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
4
4680
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) Generic observer design in C++. I have been pushing the Austria "Twin" interface for a while and more recently the multi threaded version of it "TwinMT" (see the 6129 alpha on sourceforge)
5
1480
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:
5
1234
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
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8794
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,...
1
9269
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
8216
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
6756
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
6056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2195
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.