"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > C# | Understanding the Observer Pattern

C# | Understanding the Observer Pattern

Published on 2024-07-30
Browse:982

C# | Understanding the Observer Pattern

Note
You can check other posts on my personal website: https://hbolajraf.net

The Observer Pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, and notifies them of any state changes, usually by calling one of their methods. This pattern promotes loose coupling between objects, as observers are only aware of the subject and not each other. In C#, this pattern is commonly used in event-driven programming.

Implementation

Let's understand the Observer Pattern through a detailed example in C#.

Subject Interface

First, we define an interface for the subject. This interface will contain methods for registering, unregistering, and notifying observers.

public interface ISubject
{
    void RegisterObserver(IObserver observer);
    void UnregisterObserver(IObserver observer);
    void NotifyObservers();
}

Observer Interface

Next, we define an interface for the observer. This interface will contain a method that the subject will call when it needs to notify observers.

public interface IObserver
{
    void Update();
}

Concrete Subject

Now, let's implement a concrete subject class that implements the ISubject interface.

public class ConcreteSubject : ISubject
{
    private List observers = new List();

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

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

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

Concrete Observer

Next, let's implement a concrete observer class that implements the IObserver interface.

public class ConcreteObserver : IObserver
{
    public void Update()
    {
        Console.WriteLine("Observer notified of state change.");
    }
}

Example Usage

Now, let's see how we can use these classes together.

class Program
{
    static void Main(string[] args)
    {
        ConcreteSubject subject = new ConcreteSubject();
        ConcreteObserver observer1 = new ConcreteObserver();
        ConcreteObserver observer2 = new ConcreteObserver();

        subject.RegisterObserver(observer1);
        subject.RegisterObserver(observer2);

        subject.NotifyObservers();

        subject.UnregisterObserver(observer1);

        subject.NotifyObservers();
    }
}

In this example, ConcreteSubject is the subject, and ConcreteObserver is the observer. When NotifyObservers() is called, both observers are notified of the state change. After unregistering one observer, only the remaining observer is notified.

What Next?

The Observer Pattern is a powerful way to implement communication between objects in C#. It promotes loose coupling and can be particularly useful in event-driven architectures. By understanding and implementing this pattern, you can write more maintainable and scalable code.

Release Statement This article is reproduced at: https://dev.to/hbolajraf/c-understanding-the-observer-pattern-1nm6 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3