Observer Design Pattern
 
Published: 22 Jun 2006
Unedited - Community Contributed
Abstract
In this article, Vishal demonstrates the Observer Design pattern with the help of non-software and .NET Framework examples.
by Vishal Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 22799/ 42

Overview

Good object-oriented design emphasizes both encapsulation and loose coupling.  The Observer Pattern is a mechanism for drastically reducing coupling between interacting classes.  The observer pattern should be used whenever one or more objects (observers) must track changes in the subject.  In most applications, classes do not work in isolation; they interact with many other classes.  A common scenario of class interaction occurs when one class (the Observer) needs to be notified when something changes in another (the Subject).

In this pattern, an observer registers with a subject and subsequently receives notifications and is usually encountered both in everyday life and in the software Development applications.

Its intent is to define a one-to-many dependency between objects so that when one object changes state all its dependents are notified and updated automatically

This pattern allows us to plug objects into a framework at runtime, which allows for highly flexible, extensible, and reusable software.

This pattern is also called the Publish-Subscribe pattern.

Observer pattern can be applied in any of the following situations.

1.      It can be applied when the abstraction has two aspects with one dependent on the other. Encapsulating these aspects in separate objects will increase the chance to reuse them independently.

2.      Use it when the subject object does not know exactly how many observer objects it has.

3.      Or use it when the subject object should be able to notify its observer objects without knowing who these objects are.

The following Non-software and .NET Framework examples demonstrate this pattern.

Non-software examples

In most of the airline web sites, there will be Flight Status Notification feature that you may sign up for when you want to be notified of a flight's status.  You can receive a text or a voice message with information updates on your flight or a flight you expect someone on.  You can choose to receive a voice message to your phone, a text message to a cell phone, alphanumeric pager, Personal Digital Assistant (PDA) or regular email account.

Another example can be job web sites, which usually will have job alerts feature in which job seekers will register for the particular key skill sets, experience, job category or location to receive job alerts on their e-mail accounts or mobile phones.  Whenever there are any job postings on the website specific to the registered job seeker's skill sets, experience, job category or location, an e-mail or SMS messages will be sent to the job seekers.

In the above example we can relate the job alert feature as Publisher and the job seekers as Subscribers.

.NET Framework examples

There are many instances of Observer design pattern implementation in .NET Framework.  A few of the .NET Framework classes which implement Observer design pattern are DefaultTraceListener, TextWriterTraceListener and EventLogTraceListener.  Adding these Listeners to the Listenerscollection of Trace object allows us to receive notifications and then consume them.  Below is an example that demonstrates the TextWriterTraceListener class.

Listing 1

TextWriterTraceListener listener = newTextWriterTraceListener("c:\\Myfile.txt");
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;
Trace.Write("Hi");

In the above code listing, a TextWriterTraceListener is added to the listener's collection of Trace objects.  This TextWriterTraceListener collects the output from the trace statements and writes messages to an instance of a Stream class, including output to the text file.

Next, Autoflush property is set to true, which allows Trace object to write to the output buffer and causes output buffered data to be written to the listener after every write.

In this example, we have seen that Listener acts as publisher while it maintains the output from the trace statement and notifies to the text file (Subscriber).

Another example in .NET Framework can be Cache dependency.

In the Cache dependency concept, Cache item validity will depend on the external file or on another cache item.  If a dependency changes, the cache item is invalidated and removed from the cache.  For example, Suppose XML file contains data from which an application reads data and processes and draws different views of graphs.  In such scenarios, applications cache data and will create dependencies on the file from which the data will be read.  Whenever this XML file is updated, Cache data will be removed and the application will reread data and insert again the updated copy of the latest data into the cache.

References

Conclusion

Implementation of Observer Design Pattern in .NET is simple and straightforward.  In this article we have seen some of the instances of Observer design pattern.

The study of design patterns is particularly interesting, as .NET was built with them as a foundation.  The Observer pattern seems to appear in many cases throughout the framework.

 

 



User Comments

Title: Easy to understand   
Name: Vijay
Date: 2007-06-01 9:16:14 AM
Comment:
hi,

I have browsed for many articles in the net and your articles on design patterns are really simple and easy to understand. Your work on this is really appreciated.

Thanks
Title: Good article   
Name: Vikram
Date: 2006-06-29 4:46:34 AM
Comment:
Nice article.. Vishal. written in very simple language and the use of non software examples make it very easy to grasp the fundamentals behind the pattern.great goin.. more articles on design patterns would be higly appreciated.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 3:21:51 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search