C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Demonstration of Observer pattern using a Timer


Posted Date: 16 Mar 2007    Resource Type: Code Snippets    Category: Design Patterns

Posted By: Prajnan Das       Member Level: Gold
Rating:     Points: 10



Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. The .NET Timer class can be used to demonstrate this pattern. A class called Stock which simulates a Stock quote is designed which starts a Timer internally. A parallel interface called Observer is designed. Any class which implements this interface and registers with a Stock object will be notified periodically of the stock quotes. The Stock class maintains the list of registered Observer objects in an ArrayList.

Here is the Stock Class

public class Stock
{
private ArrayList observers = new ArrayList();
private Random rand = new Random();

public Stock()
{
Timer timer = new Timer(3000);
//Register an ElapsedEventHandler which will periodically triggere GeneratePrice
timer.Elapsed +=new ElapsedEventHandler(GeneratePrice);
timer.Start();
}

public void register(Observer anObserver)
{
observers.Add(anObserver);
}

private void GeneratePrice(object sender, ElapsedEventArgs e)
{
double price = rand.NextDouble() * 100;
// We will notify the observers now
foreach(Object o in observers)
{
Observer anObserver = o as Observer;
anObserver.update(price);
}
}
}

Here is the contractual interface bewteen the Stock class and its observers

public interface Observer
{
void update(Object data);
}

Define a class called StockMonitor which implements the Observer interface

public class StockMonitor : Observer
{
public void update(Object data)
{
Console.WriteLine("Price is " + data);
}
}

Here is a little test code:

Stock s = new Stock();
StockMonitor m = new StockMonitor();
s.register(m);






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Previous Resource: Simulation of Synchronized Production and Consumption
Return to Discussion Resource Index
Post New Resource
Category: Design Patterns


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

Help Desk

Contact Us    Privacy Policy    Terms Of Use