Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

The Observer Pattern

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
19 Feb 2013CPOL 9.9K   214  
The "Observer Pattern" defines a One-To-Many dependency between objects.

----------------------------------------------------------------
Head First Design Patterns
Chapter TWO - The Observer Pattern
----------------------------------------------------------------

"PUBLISHERS + SUBSCRIBERS =  OBSERVER PATTERN"

Definition :-
The "Observer Pattern" defines a One-To-Many dependency between objects.
when one object changes state, all of its dependentsare notified and updated automatically.

ONE Subject to the MANY Observers !!!!
Subject can be anything which interests Observers.
Observers are objects who are interested in some Subject.

EXAMPLE : Newpaper Subscription where "Newspaper is Subject" and "Subscribers are Observers".
          ONE Newspaper to MANY Subscribers.

DP1 : Strive for loosely coupled designs between objects that interact.
- Loosely coupled designs allow us to build flexible system that can handle changes becoz they minimize interdependency.

Note : Observer Pattern can be implemented using Events & Delegates in .Net, .Net 4.0 has now IObservable & IObserver interfaces to
facilitate the Observer Pattern.
This Example has implementations using Custom Interface & .Net in-house IObservable interface.

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ABB
India India
Working in Software Development since 2007.
Work in .Net/VC++/COM.
work in Process Automation domain.

Comments and Discussions