Friday 3 March 2023

Observer Pattern with example

The Observer Pattern is used to maintain a list of dependents, called observers, and notify them automatically of any changes to the observed object. This is useful when you want to implement a publish-subscribe mechanism where multiple objects are interested in changes to a single object.

Here's an example implementation of the Observer pattern in C#:

public interface ISubject
{
    void Register(IObserver observer);
    void Unregister(IObserver observer);
    void NotifyObservers();
}

public interface IObserver
{
    void Update(ISubject subject);
}

public class Subject : ISubject
{
    private List<IObserver> observers = new List<IObserver>();
    private int state;

    public int State
    {
        get => state;
        set
        {
            state = value;
            NotifyObservers();
        }
    }

    public void Register(IObserver observer)
    {
        observers.Add(observer);
    }

    public void Unregister(IObserver observer)
    {
        observers.Remove(observer);
    }

    public void NotifyObservers()
    {
        foreach (var observer in observers)
        {
            observer.Update(this);
        }
    }
}

public class Observer : IObserver
{
    public void Update(ISubject subject)
    {
        if (subject is Subject s)
        {
            Console.WriteLine($"Observer updated: {s.State}");
        }
    }
}

In this implementation, we have an ISubject interface that defines methods for registering, unregistering, and notifying observers, and an IObserver interface that defines the Update method.

We also have a Subject class that implements the ISubject interface and maintains a list of observers, and an Observer class that implements the IObserver interface and updates its state when notified by the Subject instance.

To use the observer, you can simply create instances of the Subject and Observer classes, and register the observer with the subject, like this:

Subject subject = new Subject();
Observer observer = new Observer();

subject.Register(observer);
subject.State = 1; // Output: Observer updated: 1

subject.Unregister(observer);
subject.State = 2; // No output

In this example, we create an instance of the Subject and Observer classes, and register the observer with the subject by calling the Register method on the Subject instance with the Observer instance as an argument.

We then update the state of the Subject instance by setting its State property, which in turn calls the NotifyObservers method to notify all registered observers.

The Observer instance is notified and updates its state by calling the Update method, which outputs the new state.

Finally, we unregister the observer by calling the Unregister method on the Subject instance with the Observer instance as an argument, and update the state of the Subject instance again, which doesn't output anything as the observer is no longer registered.

This example demonstrates how the Observer pattern can be used to maintain a list of observers and notify them automatically of any changes to the observed object, allowing multiple objects to be notified of changes to a single object.

0 comments:

Post a Comment

Please do not enter any spam link in the message box.