Observer design Pattern
When many objects needs to recieve updates when there is a change in another objects,
Benifit =
Loosely coupled – publishers need not be aware of the type of subscriber (observer)
Flaws
The publishers( subject ) may send updates that dont matter to the subscriber (observer)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Liskov Substitution Principle
namespace ManualResetEvent_
{
interface IPublisher
{
void register(Subscriber subscriber);
void unregister(Subscriber subscriber);
void notify();
}
class publisher_ : IPublisher
{
private int no_of_items;
private List<Subscriber> subscriber_list = new List<Subscriber>();
public int inventory
{
get { return no_of_items; }
set
{
if (no_of_items < value)
notify();
no_of_items = value;
}
}
public void register(Subscriber subscriber)
{
subscriber_list.Add(subscriber);
}
public void unregister(Subscriber subscriber)
{
subscriber_list.Remove(subscriber);
}
public void notify()
{
subscriber_list.ForEach(x => x.Update());
}
}
class Subscriber : ISubscriber
{
public string Subscriber_name;
public string Name
{
get{return Subscriber_name;}
private set{}
}
public Subscriber(string name)
{
Subscriber_name =name;
}
public void Update()
{
Console.WriteLine(this.Subscriber_name+"A new product is introduced ");
}
}
interface ISubscriber
{
void Update();
}
class program
{
public static void Main()
{
Subscriber sub_1 = new Subscriber("Moto");
Subscriber sub_2 = new Subscriber("Nokia");
Subscriber sub_3 = new Subscriber("MI");
publisher_ pub1 = new publisher_();
pub1.register(sub_1);
pub1.register(sub_2);
pub1.register(sub_3);
pub1.inventory++;
pub1.unregister(sub_3);
pub1.inventory++;
Console.ReadLine();
}
}
}
Benifit =
Loosely coupled – publishers need not be aware of the type of subscriber (observer)
Flaws
The publishers( subject ) may send updates that dont matter to the subscriber (observer)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Liskov Substitution Principle
namespace ManualResetEvent_
{
interface IPublisher
{
void register(Subscriber subscriber);
void unregister(Subscriber subscriber);
void notify();
}
class publisher_ : IPublisher
{
private int no_of_items;
private List<Subscriber> subscriber_list = new List<Subscriber>();
public int inventory
{
get { return no_of_items; }
set
{
if (no_of_items < value)
notify();
no_of_items = value;
}
}
public void register(Subscriber subscriber)
{
subscriber_list.Add(subscriber);
}
public void unregister(Subscriber subscriber)
{
subscriber_list.Remove(subscriber);
}
public void notify()
{
subscriber_list.ForEach(x => x.Update());
}
}
class Subscriber : ISubscriber
{
public string Subscriber_name;
public string Name
{
get{return Subscriber_name;}
private set{}
}
public Subscriber(string name)
{
Subscriber_name =name;
}
public void Update()
{
Console.WriteLine(this.Subscriber_name+"A new product is introduced ");
}
}
interface ISubscriber
{
void Update();
}
class program
{
public static void Main()
{
Subscriber sub_1 = new Subscriber("Moto");
Subscriber sub_2 = new Subscriber("Nokia");
Subscriber sub_3 = new Subscriber("MI");
publisher_ pub1 = new publisher_();
pub1.register(sub_1);
pub1.register(sub_2);
pub1.register(sub_3);
pub1.inventory++;
pub1.unregister(sub_3);
pub1.inventory++;
Console.ReadLine();
}
}
}
Comments
Post a Comment