Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Created May 5, 2018 06:14
Show Gist options
  • Save miteshsureja/f8b4bf3b2f7376963695efbb3ea08dbb to your computer and use it in GitHub Desktop.
Save miteshsureja/f8b4bf3b2f7376963695efbb3ea08dbb to your computer and use it in GitHub Desktop.
Observer Design Pattern
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
//concrete subject
public class Infosys : Subject
{
public Infosys(string name, double price) : base(name, price) { }
}
}
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
//Observer
public interface IStockPriceWatcher
{
string Name { get; set; }
Subject Subject { get; set; }
void Update(Subject sub);
}
}
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
class Program
{
static void Main(string[] args)
{
//subjects
TCS tcs = new TCS("TCS", 3000);
Infosys infy = new Infosys("Infosys", 1100);
Wipro wipro = new Wipro("Wipro", 350);
//watchers
Watcher watcher1 = new Watcher("Watcher1");
Watcher watcher2 = new Watcher("Watcher2");
Watcher watcher3 = new Watcher("Watcher3");
Console.WriteLine(new string('-',30));
tcs.Subscribe(watcher1);
infy.Subscribe(watcher2);
wipro.Subscribe(watcher3);
Console.WriteLine(new string('-', 30));
tcs.Price = 3100;
infy.Price = 1050;
wipro.Price = 355;
Console.WriteLine(new string('-', 30));
tcs.UnSubscribe(watcher1);
infy.UnSubscribe(watcher2);
Console.WriteLine(new string('-', 30));
tcs.Price = 3170;
infy.Price = 1060;
Console.WriteLine(new string('-', 30));
tcs.Subscribe(watcher2);
infy.Subscribe(watcher1);
Console.WriteLine(new string('-', 30));
tcs.Price = 3200;
infy.Price = 1070;
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
//subject
public abstract class Subject
{
private string name;
private double price;
private List<IStockPriceWatcher> investors = new List<IStockPriceWatcher>();
public Subject(string Name, double Price)
{
name = Name;
price = Price;
}
public string Name
{
get { return name; }
}
public double Price
{
get { return price; }
set
{
if (price != value)
{
price = value;
Notify();
}
}
}
public void Subscribe(IStockPriceWatcher observer)
{
investors.Add(observer);
observer.Subject = this;
Console.WriteLine("{0} subscribed to {1}", observer.Name, name);
}
public void UnSubscribe(IStockPriceWatcher observer)
{
investors.Remove(observer);
Console.WriteLine("{0} unsubscribed from {1}", observer.Name, name );
}
public void Notify()
{
investors.ForEach(x => x.Update(this));
}
}
}
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
//concrete subject
public class TCS : Subject
{
public TCS(string name, double price) :base (name, price) { }
}
}
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
//concrete observer
public class Watcher : IStockPriceWatcher
{
public string Name { get; set; }
public Subject Subject { get ; set ; }
public Watcher(string name)
{
Name = name;
}
public void Update(Subject sub)
{
Console.WriteLine("{0}, {1} price udated to {2}",Name, Subject.Name, Subject.Price);
}
}
}
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
//concrete subject
public class Wipro : Subject
{
public Wipro(string name, double price) : base(name, price) { }
}
}
@miteshsureja
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment