Summer_feeds
Just another WordPress.com weblog

Observer Pattern/Visitor Pattern


Pattern as given in the pattern recongniztion-

Pattern recogniztion is done with the given observer.

Observer Pattern is specified to communication between layers given in the observer. Observer pattern is specified with the Visitor inside the pattern specified.

Observer Pattern components

participants

The classes and/or objects participating in this pattern are:

  • Subject (Stock)
    • knows its observers. Any number of Observer objects may observe a subject
    • provides an interface for attaching and detaching Observer objects.
  • ConcreteSubject (IBM)
    • stores state of interest to ConcreteObserver
    • sends a notification to its observers when its state changes
  • Observer (IInvestor)
    • defines an updating interface for objects that should be notified of changes in a subject.
  • ConcreteObserver (Investor)
    • maintains a reference to a ConcreteSubject object
    • stores state that should stay consistent with the subject’s
    • implements the Observer updating interface to keep its state consistent with the subject’s

// Observer pattern — Structural example
using System;
using System.Collections;namespace DoFactory.GangOfFour.Observer.Structural
{// MainApp test application

class MainApp
{
static void Main()
{
// Configure Observer pattern
ConcreteSubject s = new ConcreteSubject();

s.Attach(new ConcreteObserver(s,”X”));
s.Attach(new ConcreteObserver(s,”Y”));
s.Attach(new ConcreteObserver(s,”Z”));

// Change subject and notify observers
s.SubjectState = “ABC”;
s.Notify();

// Wait for user
Console.Read();
}
}

// “Subject”

abstract class Subject
{
private ArrayList observers = new ArrayList();

public void Attach(Observer observer)
{
observers.Add(observer);
}

public void Detach(Observer observer)
{
observers.Remove(observer);
}

public void Notify()
{
foreach (Observer o in observers)
{
o.Update();
}
}
}

// “ConcreteSubject”

class ConcreteSubject : Subject
{
private string subjectState;

// Property
public string SubjectState
{
get{ return subjectState; }
set{ subjectState = value; }
}
}

// “Observer”

abstract class Observer
{
public abstract void Update();
}

// “ConcreteObserver”

class ConcreteObserver : Observer
{
private string name;
private string observerState;
private ConcreteSubject subject;

// Constructor
public ConcreteObserver(
ConcreteSubject subject, string name)
{
this.subject = subject;
this.name = name;
}

public override void Update()
{
observerState = subject.SubjectState;
Console.WriteLine(“Observer {0}’s new state is {1}”,
name, observerState);
}

// Property
public ConcreteSubject Subject
{
get { return subject; }
set { subject = value; }
}
}
}

Output- can be taken with observer O’s passing inside

Observer {x}’s new state is {1}

Observer {y}’s new state is {2}

Observer {z}’s new state is {3}

THis is a real world example which cocerns a example with IBM prod

This real-world code demonstrates the Observer pattern in which registered investors are notified every time a stock changes value.

Hide code

// Observer pattern — Real World example
using System;
using System.Collections;namespace DoFactory.GangOfFour.Observer.RealWorld
{

// MainApp test application

class MainApp
{
static void Main()
{
// Create investors
Investor s = new Investor(“Sorros”);
Investor b = new Investor(“Berkshire”);

// Create IBM stock and attach investors
IBM ibm = new IBM(“IBM”, 120.00);
ibm.Attach(s);
ibm.Attach(b);

// Change price, which notifies investors
ibm.Price = 120.10;
ibm.Price = 121.00;
ibm.Price = 120.50;
ibm.Price = 120.75;

// Wait for user
Console.Read();
}
}

// “Subject”

abstract class Stock
{
protected string symbol;
protected double price;
private ArrayList investors = new ArrayList();

// Constructor
public Stock(string symbol, double price)
{
this.symbol = symbol;
this.price = price;
}

public void Attach(Investor investor)
{
investors.Add(investor);
}

public void Detach(Investor investor)
{
investors.Remove(investor);
}

public void Notify()
{
foreach (Investor investor in investors)
{
investor.Update(this);
}
Console.WriteLine(“”);
}

// Properties
public double Price
{
get{ return price; }
set
{
price = value;
Notify();
}
}

public string Symbol
{
get{ return symbol; }
set{ symbol = value; }
}
}

// “ConcreteSubject”

class IBM : Stock
{
// Constructor
public IBM(string symbol, double price)
: base(symbol, price)
{
}
}

// “Observer”

interface IInvestor
{
void Update(Stock stock);
}

// “ConcreteObserver”

class Investor : IInvestor
{
private string name;
private Stock stock;

// Constructor
public Investor(string name)
{
this.name = name;
}

public void Update(Stock stock)
{
Console.WriteLine(“Notified {0} of {1}’s ” +
“change to {2:C}”, name, stock.Symbol, stock.Price);
}

// Property
public Stock Stock
{
get{ return stock; }
set{ stock = value; }
}
}
}

Output
Advertisements

No Responses to “Observer Pattern/Visitor Pattern”

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: