Observer Pattern Code Review
Define the Subject, Blogs and Simulator Objects
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
The Subject class (object) manages all subscriptions for a particular event.
class Subject
{
public delegate void Callback(Blogs blog);
Dictionary<String, Callback> notify = new Dictionary<String, Callback>(); Simulator simulator = new Simulator();
const Int32 speed = 4000;
private void Register(String blogger) { if (!this.notify.ContainsKey(blogger)) { this.notify[blogger] = delegate { }; } } private void Run() { foreach (Blogs blog in simulator) { this.Register(blog.Name); this.notify[blog.Name](blog); Thread.Sleep(speed); } } public void Attach(String blogger, Callback update) { this.Register(blogger); notify[blogger] += update; } public void Detach(String blogger, Callback update) { notify[blogger] -= update; } public void Go() { new Thread(new ThreadStart(this.Run)).Start(); } }
Dictionary<String, Callback> notify = new Dictionary<String, Callback>(); Simulator simulator = new Simulator();
const Int32 speed = 4000;
private void Register(String blogger) { if (!this.notify.ContainsKey(blogger)) { this.notify[blogger] = delegate { }; } } private void Run() { foreach (Blogs blog in simulator) { this.Register(blog.Name); this.notify[blog.Name](blog); Thread.Sleep(speed); } } public void Attach(String blogger, Callback update) { this.Register(blogger); notify[blogger] += update; } public void Detach(String blogger, Callback update) { notify[blogger] -= update; } public void Go() { new Thread(new ThreadStart(this.Run)).Start(); } }
When the event occurs, the Subject class (object) will invoke the Update() method in the Observer class (object).
The Blogs class (object) represents events that can be subscribed to.
class Blogs
{
public String Name { get; set; }
public String Topic { get; set; }
public Blogs(String name, String topic) { this.Name = name; this.Topic = topic; } }
public Blogs(String name, String topic) { this.Name = name; this.Topic = topic; } }
The Simulator class (object) creates the events that can be subscribed to.
class Simulator : IEnumerable
{
Blogs[] bloggers = {
new Blogs("Jim", "UML diagrams"),
new Blogs("Eric", "Iterators"),
new Blogs("Eric", "Extension Methods"),
new Blogs("Judith", "Delegates"),
new Blogs("Eric", "Type interface"),
new Blogs("Jim", "Threads"),
new Blogs("Eric", "Lamda expressions"),
new Blogs("Judith", "Anonymous properties"),
new Blogs("Eric", "Generic delegates"),
new Blogs("Jim", "Efficiency")
};
IEnumerator IEnumerable.GetEnumerator() { foreach (Blogs blog in bloggers) { yield return blog; } } }
IEnumerator IEnumerable.GetEnumerator() { foreach (Blogs blog in bloggers) { yield return blog; } } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!