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.
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.