<<< OBSERVER CODE e.g.

The Observer pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, that are notified of any state changes. This pattern is commonly used to implement distributed event handling systems. Here's an example of how you can implement the Observer pattern in C#:

using System;

using System.Collections.Generic;


// Subject interface

public interface ISubject

{

    void Attach(IObserver observer);

    void Detach(IObserver observer);

    void Notify();

}


// ConcreteSubject class

public class ConcreteSubject : ISubject

{

    private List<IObserver> observers = new List<IObserver>();

    private string state;


    public string 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 (var observer in observers)

        {

            observer.Update(this);

        }

    }

}


// Observer interface

public interface IObserver

{

    void Update(ISubject subject);

}


// ConcreteObserver class

public class ConcreteObserver : IObserver

{

    private string observerState;


    public void Update(ISubject subject)

    {

        if (subject is ConcreteSubject concreteSubject)

        {

            observerState = concreteSubject.State;

            Console.WriteLine($"Observer updated with state: {observerState}");

        }

    }

}


// Client code

class Program

{

    static void Main()

    {

        ConcreteSubject subject = new ConcreteSubject();


        ConcreteObserver observer1 = new ConcreteObserver();

        ConcreteObserver observer2 = new ConcreteObserver();


        subject.Attach(observer1);

        subject.Attach(observer2);


        subject.State = "New State"; // This will trigger an update for both observers

    }

}

In this example:

ISubject is the interface for the subject, which declares methods for attaching, detaching, and notifying observers.

ConcreteSubject is the concrete implementation of ISubject that maintains a list of observers and notifies them of state changes.

IObserver is the interface for the observers, which declares the Update method to be called when the subject's state changes.

ConcreteObserver is the concrete implementation of IObserver that updates its state when notified by the subject.

In the Main method, a ConcreteSubject is created, and two ConcreteObserver instances are attached to it. When the state of the subject changes, both observers are notified and updated.

This allows you to create a flexible system where multiple observers can react to changes in the state of a subject without the subject being dependent on the concrete classes of its observers.

#AbdurRahimRatulAliKhan #ARRAK #Code #Programming #CodeDescription #Observer #DesignPatterns