Lädt...


🔧 C# Design Pattern: Observer


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The Observer pattern is used to automatically notify multiple objects when the state of another object changes. It defines a "one-to-many" relationship between objects, where when one object (the subject) changes, all its observers are notified. It is useful in situations where multiple parts of the system need to react to changes in an object, such as in event or notification systems.

C# Code Example:

// Observer interface
public interface IObserver
{
    void Update(string state);
}

// Subject interface
public interface ISubject
{
    void AddObserver(IObserver observer);
    void RemoveObserver(IObserver observer);
    void NotifyObservers();
}

// Concrete Subject implementation
public class ConcreteSubject : ISubject
{
    private List<IObserver> _observers = new List<IObserver>();
    private string _state;

    public string State
    {
        get { return _state; }
        set
        {
            _state = value;
            NotifyObservers();
        }
    }

    public void AddObserver(IObserver observer)
    {
        _observers.Add(observer);
    }

    public void RemoveObserver(IObserver observer)
    {
        _observers.Remove(observer);
    }

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

// Concrete Observers
public class ConcreteObserver1 : IObserver
{
    public void Update(string state)
    {
        Console.WriteLine($"Observer 1 has been notified. New state: {state}");
    }
}

public class ConcreteObserver2 : IObserver
{
    public void Update(string state)
    {
        Console.WriteLine($"Observer 2 has been notified. New state: {state}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create the subject
        ConcreteSubject subject = new ConcreteSubject();

        // Create and add observers
        ConcreteObserver1 observer1 = new ConcreteObserver1();
        ConcreteObserver2 observer2 = new ConcreteObserver2();

        subject.AddObserver(observer1);
        subject.AddObserver(observer2);

        // Change the subject's state
        subject.State = "State A";
        subject.State = "State B";

        // Remove an observer and change the state
        subject.RemoveObserver(observer1);
        subject.State = "State C";
    }
}

Code Explanation:
In this example, the ConcreteSubject class maintains a list of observers and notifies all registered observers when the state changes. The NotifyObservers method iterates over the list of observers and calls the Update method on each one. The observers ConcreteObserver1 and ConcreteObserver2 react to the subject's state changes.

Conclusion:
The Observer pattern is useful when multiple parts of the system need to be automatically informed about changes in an object's state. It reduces coupling between the subject and the observers, making the system more flexible.

Source code: GitHub

...

🐧 Observer Pattern: Was steckt hinter dem Observer Design Pattern?


📈 60.94 Punkte
🐧 Server

🔧 Observer-Pattern | Javascript Design Pattern Simplified | Part 3


📈 44 Punkte
🔧 Programmierung

🔧 Design Pattern #3 - Observer Pattern


📈 44 Punkte
🔧 Programmierung

🔧 Pub/Sub pattern vs Observer Pattern: what's the difference?


📈 38.75 Punkte
🔧 Programmierung

🔧 C# Design Pattern: Observer


📈 33.09 Punkte
🔧 Programmierung

🔧 C# Design Pattern: Observer (Portugues)


📈 33.09 Punkte
🔧 Programmierung

🔧 Building a Reactive Store in React Using the Observer Design Pattern and Custom Hook


📈 33.09 Punkte
🔧 Programmierung

🔧 Understanding the Observer Design Pattern in Java


📈 33.09 Punkte
🔧 Programmierung

🔧 Kotlin Design Patterns: Simplifying the Observer Pattern


📈 33.09 Punkte
🔧 Programmierung

🔧 Using the Observer Pattern in Payroll Processing


📈 27.84 Punkte
🔧 Programmierung

🔧 Real-Time Notifications with the Observer Pattern: A Practical Guide


📈 27.84 Punkte
🔧 Programmierung

🔧 C# | Understanding the Observer Pattern


📈 27.84 Punkte
🔧 Programmierung

🔧 Understanding the Subject-Observer Pattern with RxDart in Dart


📈 27.84 Punkte
🔧 Programmierung

🔧 C# Delegates In Practice — Implementing Observer Pattern With Delegates


📈 27.84 Punkte
🔧 Programmierung

🔧 JavaScript Designs — Observer Pattern


📈 27.84 Punkte
🔧 Programmierung

🔧 Observer Pattern for Beginners


📈 27.84 Punkte
🔧 Programmierung

🔧 Mastering Real-Time Magic: The Observer Pattern


📈 27.84 Punkte
🔧 Programmierung

🔧 Observer Pattern


📈 27.84 Punkte
🔧 Programmierung

🔧 Implementing Chain of Responsibility Pattern in C# : Middleware's Design Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #7 - Builder Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #6 - Composite Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #5 - Adapter Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Module-Pattern | Javascript Design Pattern Simplified | Part 4


📈 27.06 Punkte
🔧 Programmierung

🔧 Decorator-Pattern | Javascript Design Pattern Simplified | Part 5


📈 27.06 Punkte
🔧 Programmierung

🔧 Factory-Pattern | Javascript Design Pattern Simplified | Part 2


📈 27.06 Punkte
🔧 Programmierung

🔧 Singleton-Pattern | Javascript Design Pattern Simplified | Part 1


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #4 - Publisher/Subscriber Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #2 - Facade Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Intersection Observer: Ein mächtiges Tool für effizientes Web-Design 🚀


📈 22.19 Punkte
🔧 Programmierung

🔧 Intersection Observer: A powerful tool for efficient web design 🚀


📈 22.19 Punkte
🔧 Programmierung

🔧 JavaScript Design Patterns - Behavioral - Observer


📈 22.19 Punkte
🔧 Programmierung

⚠️ #0daytoday #Pdfium - Out-of-Bounds Read with Shading Pattern Backed by Pattern Colorspace Exploit [#0day #Exploit]


📈 21.81 Punkte
⚠️ PoC

⚠️ [dos] Pdfium - Out-of-Bounds Read with Shading Pattern Backed by Pattern Colorspace


📈 21.81 Punkte
⚠️ PoC

🔧 Webkul pattern question advance pattern for interview with python


📈 21.81 Punkte
🔧 Programmierung

🔧 Service: O pattern que virou anti-pattern


📈 21.81 Punkte
🔧 Programmierung

matomo