- Behavioral pattern
- The intent of this pattern is to define a one to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Example Observables in angular, service communications in microservice architectures
- Observer:- defines an updating interface for objects that should be notified of changes in a subject.
- Subject :- knows its observers. Provides an interface for attaching and detaching them.
- ConcreteObserver :- store state that must remain consists with the subject’s state. They implement the Observer updating interface to keep state consistent.
- ConcreteSubject:- stores state of interest to ConcreteObserver objects, and sends a notification to its Observers when its state changes.
- Use case :- When a change to one object requires changing others, and you dont know in advance how many objects need to be changed
- It allows subjects and observers to vary independently : subclasses can be added and change without having to change others: OC principle is not broken
- Subject and Observers are loosely coupled : Open closed principle is not broken
- Some of the related patterns:- Chain of Responsibility, Command, Mediator
namespace observerpattern
{
public interface ISubject
{
void Register(IObserver observer);
void UnRegister(IObserver observer);
void Notify();
}
public interface IObserver
{
void Update(ISubject subject);
}
public class ZeeNews : IObserver
{
private readonly string _newsChannelName;
public ZeeNews(string newsChannelName)
{
_newsChannelName = newsChannelName;
}
public void Update(ISubject subject)
{
if (subject is EarthQuakeMeasurement earthQuakeMeasurement)
{
Console.WriteLine($"Breakihng news from {_newsChannelName} :: Danger of EarthQuake, seismo records {earthQuakeMeasurement.SeismoMagnitude} , be safe.");
}
}
}
public class AajTakNews : IObserver
{
private readonly string _newsChannelName;
public AajTakNews(string newsChannelName)
{
_newsChannelName = newsChannelName;
}
public void Update(ISubject subject)
{
if(subject is EarthQuakeMeasurement earthQuakeMeasurement)
{
Console.WriteLine($"Big bulletin news from {_newsChannelName} :: Danger of EarthQuake , seismo records {earthQuakeMeasurement.SeismoMagnitude} , be safe.");
}
}
}
public class EarthQuakeMeasurement : ISubject
{
private decimal _seismoMagnitude;
public decimal SeismoMagnitude {
get { return _seismoMagnitude; }
set
{
_seismoMagnitude = value;
Notify();
}
}
private readonly List _observers = new();
public void Register(IObserver observer)
{
_observers.Add(observer);
}
public void UnRegister(IObserver observer)
{
_observers.Remove(observer);
}
public void Notify()
{
_observers.ForEach(o => o.Update(this));
}
}
}
PROGRAM.CS
using observerpattern;
EarthQuakeMeasurement publisherObj = new EarthQuakeMeasurement();
publisherObj.Register(new AajTakNews("AAJ TAK"));
publisherObj.Register(new ZeeNews("ZEE NEWS"));
publisherObj.SeismoMagnitude = 7.1m;
OUTPUT
Big bulletin news from AAJ TAK :: Danger of EarthQuake , seismo records 7.1 , be safe.
Breakihng news from ZEE NEWS :: Danger of EarthQuake, seismo records 7.1 , be safe.
GITHUB