Observer Pattern Code Review
Define the Pattern Interface and Handler Object
Code Download
- Download Description:observer pattern download
- .NET Framework:3.5
- .NET Language:C#
- Date Published:2009-07-01
- Download Size:18 KB
Code Review
- Observer Pattern Code Review: Define the Pattern Interface and Handler Object
- Observer Pattern Code Review: Define the Subject, Blogs and Simulator Objects
- Observer Pattern Code Review: Define the Windows Form Graphical User Interface
- Observer Pattern Code Review: Use the Observer Pattern
Code Walkthrough
Define the IObserver interface to be used as the "blueprint" for the main Observer classes.
interface IObserver
{
void Update(Blogs state);
}
The Observer class (object) represents an object that wants to be notified automatically when a particular event occurs.
class Observer : IObserver
{
String subscriber;
Subject blogs;
Interact visuals;
public Observer(Subject subject, String name) { this.blogs = subject; this.subscriber = name; visuals = new Interact(this.subscriber, this.Input); new Thread((ParameterizedThreadStart)delegate(object o) { Application.Run(visuals); }).Start(this);
while (visuals == null || visuals.IsHandleCreated) { Application.DoEvents(); Thread.Sleep(100); } blogs.Attach("Jim", this.Update); blogs.Attach("Eric", this.Update); blogs.Attach("Judith", this.Update); }
public void Update(Blogs state) { visuals.Output(String.Format("Blog from {0} on {1}", state.Name, state.Topic)); } public void Input(Object source, EventArgs e) { if (source == visuals.subscribeButton) { blogs.Attach(visuals.messageBox.Text, this.Update); visuals.wall.AppendText(String.Format("Subscribed to {0}\r\n", visuals.messageBox.Text)); } else { if (source == visuals.unSubscribeButton) { blogs.Detach(visuals.messageBox.Text, this.Update); visuals.wall.AppendText(String.Format("Unsubscribed from {0}\r\n", visuals.messageBox.Text)); } } } }
public Observer(Subject subject, String name) { this.blogs = subject; this.subscriber = name; visuals = new Interact(this.subscriber, this.Input); new Thread((ParameterizedThreadStart)delegate(object o) { Application.Run(visuals); }).Start(this);
while (visuals == null || visuals.IsHandleCreated) { Application.DoEvents(); Thread.Sleep(100); } blogs.Attach("Jim", this.Update); blogs.Attach("Eric", this.Update); blogs.Attach("Judith", this.Update); }
public void Update(Blogs state) { visuals.Output(String.Format("Blog from {0} on {1}", state.Name, state.Topic)); } public void Input(Object source, EventArgs e) { if (source == visuals.subscribeButton) { blogs.Attach(visuals.messageBox.Text, this.Update); visuals.wall.AppendText(String.Format("Subscribed to {0}\r\n", visuals.messageBox.Text)); } else { if (source == visuals.unSubscribeButton) { blogs.Detach(visuals.messageBox.Text, this.Update); visuals.wall.AppendText(String.Format("Unsubscribed from {0}\r\n", visuals.messageBox.Text)); } } } }
This object subscribes to a particular event. When the event occurs, the Update() method is called on the Observer class (object).
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!