3

I read a book about the observer pattern. It gives the following example:

Class clock, with method time()

Class message, which 3 class inherit from it: Fax, Mail, Memo.

The application wants to send a message when 12AM arrived.

The book recommends observer pattern. It says that if we add new class: VideoMsg which extends message, it would affect the implementation of class Clock. But I didn't understand why. The class Clock would hold collection of message's object, and if we will add a new inherit class, It wouldn't change the clock class.

I would appreciate if someone will explain the example above, or give a better example.

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Adam Sh
  • 8,137
  • 22
  • 60
  • 75
  • did you search here on so for observer pattern? http://stackoverflow.com/search?q=observer+pattern – albertjan Apr 26 '12 at 10:19
  • check either of these http://stackoverflow.com/questions/1249517/super-simple-example-of-c-sharp-observer-observable-with-delegates OR http://www.codeproject.com/Articles/6384/Observer-Pattern-in-NET – userGS Apr 26 '12 at 10:20
  • @userGS: I understood how implement observer pattren. I didn't understand why we need it. – Adam Sh Apr 26 '12 at 10:21
  • to achieve publisher-subscriber functionality we use observer pattern – Balaswamy Vaddeman Apr 26 '12 at 10:23
  • so is your question about "didn't understand why new class will affect implementation of Clock", or even "didn't understand the purpose of Observer" ? – Adrian Shum Apr 26 '12 at 10:25
  • 1
    why is this tagged with both c# and java? in c# you have events, there is no need for the observer pattern –  Apr 26 '12 at 10:27
  • @alegen: as I understood, in c# I have the IObservable interface – Adam Sh Apr 26 '12 at 10:31
  • @AdrianShum: I didn't understand the purpose of Observe in this case, and I will happy to get an example that would exaplain me where I got a benefits of using the observer pattren – Adam Sh Apr 26 '12 at 10:32
  • @AdamSh i didn`t say the pattern cannot be implemented; i said that .net has events and are specifically designed for this. it is basically the same concept but code wise it is simpler to implement the logic –  Apr 26 '12 at 10:55

5 Answers5

3

in Simple words ,to make you understand Observer pattern

if 1000 people are subscribed to a daily news paper.

Publisher will send a copy to his subscribers whenever a new copy arrives i.e. on daily basis.

Same way One class will send information to its observers whenever it gets new information.

Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
1

Because the new new class, which is an observer, will only call the subscribe method in the clock instance, and when the clock hits 12AM, it will call the notifyObservers method, which will iterate over the subscribers and call their notify method telling them that it's 12AM now.

And they will react to this each in their own way, by displaying a message to the user or ringing or whatever, and you will not have to change anything in you clock implementation.

Darth Plagueis
  • 910
  • 3
  • 21
  • 39
0

Honestly it is hard to give answer, as there isn't much implementation detail in the example.

In the Observer pattern, the "Observed object" (Subject) will hold a reference to Observers, and Subject is normally responsible for constructing the "message" to send to Observers. That means, if you need to notify Observers of a new message, the implementation of Subject needs to be changed because it needs to be aware of how the new message is constructed.

However, this is only describing the normal observer pattern implementation. There are too may situations or variants that can make, as you said, Subject not dependent upon messages.

Richard Fawcett
  • 2,799
  • 1
  • 29
  • 36
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

The essence is:

The observees do not need to hold a reference of the observer, just provide a handler which describe the behavior when it be notified.

Combine Observer pattern with the OO theory Inheritance and Polymorphism, you will see how FLEXIBLE your code will be.

Lyn
  • 699
  • 1
  • 7
  • 17
0

Beside missing details i would guess that 'messages' observe clock objects to trigger 'actions' on time. Depending on 'how' (observing) ? this is implemented it could mean changing clock is necessary. The observer pattern is the 'we call you - don't call us' pattern. It basicly prevents the expensive busy waiting looping.

hburde
  • 1,441
  • 11
  • 6