2007 |
Section 7, Chapter 3 |
Observer Pattern
Concept
The Observer pattern facilitates communication between a parent class and any dependent child classes, allowing changes to the state of the parent class to be sent to the dependent child classes. The class relationship is one-to-many between the class and all its dependents.
Use
Observers generally monitor the state of a class they are linked to and get information from that class when changes occur that they are concerned about. If we had a class that linked into many classes, and those classes wished to know about changes within it, we might use this pattern.
Design
The pattern generally consists of two base classes. The first is called the Subject class, and this class acts as the notification engine. The Observer classes act as receivers of the subject notifications.
Illustration
public string Title
{
get{return _title;}
set
{
_title = value;
Notify();
}
}
Notice that the Notify() method is called when the Title accessor is changed. This allows all attached observer objects to be notified of the change.
December 2007 |
Section 3, Chapter 9 |
Note: the relation between Subject and Observer, here Observer maintains a reference to instance of Subject. |
Use
- There are aspects to an abstraction that can vary independently.
- Changes in one object need to be propagated to a selection of other objects, not all of them.
- The object sending the changes does not need to know about the receivers.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
class ObserverPattern {
// State type
public class Blogs {
public string Name {get; set;}
public string Topic {get; set;}
public Blogs (string name, string topic) {
Name = name;
Topic = topic;
}
}
public delegate void Callback (Blogs blog);
// The Subject runs in a thread and changes its state
// independently by calling the Iterator
// At each change, it notifies its Observers
// The Callbacks are in a collection based on blogger name
class Subject {
Dictionary <string,Callback> Notify = new Dictionary <string,Callback> ( );
Simulator simulator = new Simulator( );
const int speed = 4000;
public void Go( ) {
new Thread(new ThreadStart(Run)).Start( );
}
void Run ( ) {
foreach (Blogs blog in simulator) {
Register(blog.Name); // if necessary
Notify[blog.Name](blog); // publish changes
Thread.Sleep(speed); // milliseconds
}
}
// Adds to the blogger list if unknown
void Register (string blogger) {
if (!Notify.ContainsKey(blogger)) {
Notify[blogger] = delegate {};
}
}
public void Attach(string blogger, Callback Update) {
Register(blogger);
Notify[blogger] += Update;
}
public void Detach(string blogger, Callback Update) {
// Possible problem here
Notify[blogger] -= Update;
}
}
class Interact : Form {
public TextBox wall;
public Button subscribeButton, unsubscribeButton ;
public TextBox messageBox;
string name;
public Interact(string name, EventHandler Input) {
Control.CheckForIllegalCrossThreadCalls = true;
// wall must be first!
this.name = name;
wall = new TextBox( );
wall.Multiline = true;
wall.Location = new Point(0, 30);
wall.Width = 300;
wall.Height = 200;
wall.AcceptsReturn = true;
wall.Dock = DockStyle.Fill;
this.Text = name;
this.Controls.Add(wall);
// Panel must be second
Panel p = new Panel( );
messageBox = new TextBox( );
messageBox.Width = 120;
p.Controls.Add(messageBox);
subscribeButton = new Button( );
subscribeButton.Left = messageBox.Width;
subscribeButton.Text = "Subscribe";
subscribeButton.Click += new EventHandler(Input);
p.Controls.Add(subscribeButton);
unsubscribeButton = new Button( );
unsubscribeButton.Left = messageBox.Width+subscribeButton.Width;
unsubscribeButton.Text = "Unsubscribe";
unsubscribeButton.Click += new EventHandler(Input);
p.Controls.Add(unsubscribeButton);
p.Height = subscribeButton.Height;
p.Height = unsubscribeButton.Height;
p.Dock = DockStyle.Top;
this.Controls.Add(p);
}
public void Output(string message) {
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate( ) { Output(message); });
else {
wall.AppendText(message + "\r\n");
this.Show( );
}
}
}
// Useful if more observer types
interface IObserver {
void Update(Blogs state);
}
class Observer : IObserver {
string name;
Subject blogs;
Interact visuals;
public Observer (Subject subject, string name) {
this.blogs = subject;
this.name = name;
visuals = new Interact(name,Input);
new Thread((ParameterizedThreadStart) delegate(object o) {
Application.Run(visuals);
}).Start(this);
// Wait to load the GUI
while (visuals == null || !visuals.IsHandleCreated) {
Application.DoEvents( );
Thread.Sleep(100);
}
blogs.Attach("Jim",Update);
blogs.Attach("Eric",Update);
blogs.Attach("Judith",Update);
}
public void Input(object source, EventArgs e) {
// Subscribe to the specified blogger
if (source == visuals.subscribeButton) {
blogs.Attach(visuals.messageBox.Text, Update);
visuals.wall.AppendText("Subscribed to "+visuals.messageBox.Text+"\r\n");
} else
// Unsubscribe from the blogger
if (source == visuals.unsubscribeButton) {
blogs.Detach(visuals.messageBox.Text, Update);
visuals.wall.AppendText("Unsubscribed from "+visuals.messageBox.Text+"\r\n");
}
}
public void Update(Blogs blog) {
visuals.Output("Blog from "+blog.Name+" on "+blog.Topic);
}
}
// Iterator to supply the data
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 inference"),
new Blogs("Jim","Threads"),
new Blogs("Eric","Lamda expressions"),
new Blogs("Judith","Anonymous properties"),
new Blogs("Eric","Generic delegates"),
new Blogs("Jim","Efficiency")};
public IEnumerator GetEnumerator ( ) {
foreach( Blogs blog in bloggers )
yield return blog;
}
}
static void Main ( ) {
Subject subject = new Subject( );
Observer Observer = new Observer(subject,"Thabo");
Observer observer2 = new Observer(subject,"Ellen");
subject.Go( );
}
}
2016 |
Chapter 2 |
GoF Definition: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
interface IObserver
{
void update(int i);
}
class Observer1 implements IObserver
{
@Override
public void update(int i)
{
System.out.println("Observer1: myValue in Subject is now: "+i);
}
}
class Observer2 implements IObserver
{
@Override
public void update(int i)
{
System.out.println("Observer2: observes ->myValue is changed in Subject to :"+i);
}
}
interface ISubject
{
void register(IObserver o);
void unregister(IObserver o);
void notifyObservers(int i);
}
class Subject implements ISubject
{
private int myValue;
public int getMyValue() {
return myValue;
}
public void setMyValue(int myValue) {
this.myValue = myValue;
//Notify observers
notifyObservers(myValue);
}
List<IObserver> observersList=new ArrayList<IObserver>();
@Override
public void register(IObserver o)
{
observersList.add(o);
}
@Override
public void unregister(IObserver o)
{
observersList.remove(o);
}
@Override
public void notifyObservers(int updatedValue)
{
for(int i=0;i<observersList.size();i++)
{
observersList.get(i).update(updatedValue);
}
}
}
class ObserverPatternModifiedEx
{
public static void main(String[] args)
{
System.out.println("*** Modified Observer Pattern Demo***\n");
Subject sub = new Subject();
Observer1 ob1 = new Observer1();
Observer2 ob2 = new Observer2();
sub.register(ob1);
sub.register(ob2);
sub.setMyValue(5);
System.out.println();
sub.setMyValue(25);
System.out.println();
//unregister ob1 only
sub.unregister(ob1);
//Now only ob2 will observe the change
sub.setMyValue(100);
}
}
March 25, 2002 |
Chapter 8 |