Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Observer design pattern.
The Observer design pattern is a behavioral design pattern that connects an object to several dependent classes. This pattern provides the possibility that when the state of an object changes, its dependent objects are automatically notified and updated. The Observer pattern is useful when you need to maintain consistency between related objects without tightly coupling them.
The Observer pattern consists of various components:
Below is an example of how to implement the Observer pattern in C#:
interface IObserver
{
void Update();
}
class Subject
{
private List<IObserver> observers = new List<IObserver>();
private int state;
public int State
{
get { return state; }
set
{
state = value;
Notify();
}
}
public void Attach(IObserver observer)
{
observers.Add(observer);
}
public void Detach(IObserver observer)
{
observers.Remove(observer);
}
public void Notify()
{
foreach (IObserver observer in observers)
{
observer.Update();
}
}
}
class ConcreteObserver : IObserver
{
private string name;
private Subject subject;
public ConcreteObserver(string name, Subject subject)
{
this.name = name;
this.subject = subject;
}
public void Update()
{
Console.WriteLine("{0} has received an update: {1}", name, subject.State);
}
}
class Program
{
static void Main(string[] args)
{
Subject subject = new Subject();
ConcreteObserver observer1 = new ConcreteObserver("Observer 1", subject);
ConcreteObserver observer2 = new ConcreteObserver("Observer 2", subject);
subject.Attach(observer1);
subject.Attach(observer2);
subject.State = 5;
subject.Detach(observer2);
subject.State = 10;
Console.ReadKey();
}
}
In the example above, we have a Subject class that has mutable state. You also have a list of Observers that must be obeyed during the state change. When the state of the Subject changes, all connected Observers will receive these changes by calling the Update method. The connected Observer classes each have their own commands that they execute when they receive an update.
The Observer design pattern is useful when you need to maintain relationships between related objects without connecting them directly. When the state of an object changes, this change will be communicated to all connected objects and each of them will update themselves according to their own commands. By using the Observer pattern, you can create a more flexible and scalable design that separates the concerns of different components.
To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.
I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.
leave a comment