DEV Community

Muhammad Salem
Muhammad Salem

Posted on

The Observer Patter - A Practical Approach.

The Observer pattern solves the problem of loosely coupled communication between objects. This means that objects can be notified of changes in state without having to know the specific details of the object they are observing.

Here's a breakdown of the problem and how the Observer pattern addresses it:

Problem:

  • Tight Coupling: If objects are directly coupled, changes to one object can have unintended consequences on others. This makes the code harder to maintain and extend.
  • Notification Complexity: Manually managing notifications between objects can be complex and error-prone, especially in large systems.

Observer Pattern Solution:

  • Decoupling: The Observer pattern introduces a subject and one or more observers. The subject maintains a list of observers and notifies them when its state changes. The observers don't need to know anything about the subject's implementation, only that they will be notified of changes.
  • Notification Mechanism: The subject provides a method for observers to register and unregister themselves. When the subject's state changes, it calls a method on all registered observers, passing them the necessary information.

Benefits of the Observer Pattern:

  • Improved Maintainability: Loose coupling makes the code easier to understand, modify, and extend.
  • Enhanced Flexibility: New observers can be added or removed without affecting the existing code.
  • Simplified Communication: The pattern handles the notification process, reducing the complexity of managing communication between objects.

Let's go through a Before and After scenario to illustrate the benefits of the Observer pattern. We'll use a simple example where a Weather Station notifies different display devices (e.g., a phone display, a window display) when the temperature changes.

Scenario: Weather Station Notifying Displays

Before: Without the Observer Pattern (Tightly Coupled Design)

In this design, the WeatherStation directly interacts with all display devices. Every time the temperature changes, the weather station has to notify each display device individually. This creates a tightly coupled system, making it difficult to add new displays or modify existing ones.

class PhoneDisplay {
    public void update(int temperature) {
        System.out.println("Phone Display: Temperature updated to " + temperature + "°C");
    }
}

class WindowDisplay {
    public void update(int temperature) {
        System.out.println("Window Display: Temperature updated to " + temperature + "°C");
    }
}

class WeatherStation {
    private int temperature;
    private PhoneDisplay phoneDisplay;
    private WindowDisplay windowDisplay;

    public WeatherStation(PhoneDisplay phoneDisplay, WindowDisplay windowDisplay) {
        this.phoneDisplay = phoneDisplay;
        this.windowDisplay = windowDisplay;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
        phoneDisplay.update(temperature);
        windowDisplay.update(temperature);
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage:

PhoneDisplay phoneDisplay = new PhoneDisplay();
WindowDisplay windowDisplay = new WindowDisplay();
WeatherStation weatherStation = new WeatherStation(phoneDisplay, windowDisplay);

weatherStation.setTemperature(25);  // Both displays are updated
Enter fullscreen mode Exit fullscreen mode

Problems with This Design:

  1. Tight Coupling: The WeatherStation class knows about both PhoneDisplay and WindowDisplay. If you want to add or remove displays, you need to modify the WeatherStation class, violating the Open/Closed Principle.
  2. Limited Flexibility: Adding new types of displays (e.g., a TVDisplay) requires changes to the WeatherStation class.
  3. Code Maintenance Issues: As the number of displays grows, the WeatherStation class becomes harder to manage and maintain.

After: Using the Observer Pattern (Loosely Coupled Design)

In this design, the WeatherStation class doesn't know anything about the specific display devices. It only knows that it has a list of observers, and it will notify them when the temperature changes. The display devices are observers that register themselves with the weather station.

Step 1: Define the Observer Interface
interface Observer {
    void update(int temperature);
}
Enter fullscreen mode Exit fullscreen mode
Step 2: Implement the Observers (Display Devices)
class PhoneDisplay implements Observer {
    @Override
    public void update(int temperature) {
        System.out.println("Phone Display: Temperature updated to " + temperature + "°C");
    }
}

class WindowDisplay implements Observer {
    @Override
    public void update(int temperature) {
        System.out.println("Window Display: Temperature updated to " + temperature + "°C");
    }
}
Enter fullscreen mode Exit fullscreen mode
Step 3: Define the Subject Interface (WeatherStation)
interface Subject {
    void registerObserver(Observer observer);
    void removeObserver(Observer observer);
    void notifyObservers();
}
Enter fullscreen mode Exit fullscreen mode
Step 4: Implement the WeatherStation (Subject)
import java.util.ArrayList;
import java.util.List;

class WeatherStation implements Subject {
    private List<Observer> observers;
    private int temperature;

    public WeatherStation() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature);
        }
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
        notifyObservers();  // Notify all observers about the change
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage:

WeatherStation weatherStation = new WeatherStation();

PhoneDisplay phoneDisplay = new PhoneDisplay();
WindowDisplay windowDisplay = new WindowDisplay();

// Register displays as observers
weatherStation.registerObserver(phoneDisplay);
weatherStation.registerObserver(windowDisplay);

weatherStation.setTemperature(25);  // Both displays are updated

// You can easily add a new display
Observer tvDisplay = new Observer() {
    @Override
    public void update(int temperature) {
        System.out.println("TV Display: Temperature updated to " + temperature + "°C");
    }
};
weatherStation.registerObserver(tvDisplay);

weatherStation.setTemperature(30);  // All three displays are updated
Enter fullscreen mode Exit fullscreen mode

Benefits of the Observer Pattern:

  1. Loose Coupling: The WeatherStation class doesn't need to know about specific display types. It only knows about the Observer interface, so new displays can be added without modifying the WeatherStation class.

  2. Enhanced Flexibility: You can easily add or remove observers (displays) without changing the WeatherStation class. For example, adding a TVDisplay doesn't require any changes to the core weather station logic.

  3. Improved Maintainability: The code is easier to maintain and extend because the subject and observers are decoupled. You can update the notification logic or add new observer types independently.

  4. Scalability: The system can grow with more observers, and the notification mechanism remains the same. The WeatherStation doesn’t need to worry about how many observers it has or what they do with the updates.

By applying the Observer pattern, we transformed a tightly coupled system into a more maintainable and flexible design. This pattern is highly effective in scenarios where you need to notify multiple objects about changes in another object’s state without creating strong dependencies between them.

When deciding between leveraging C# events or applying the Observer pattern, it's important to understand the distinctions, use cases, and trade-offs between these two approaches. Both are related to the concept of the Observer pattern but have different practical implementations and implications in C#.

1. C# Events vs. Observer Pattern

C# Events:

  • What are C# Events?

    C# events are a language-specific feature that allows one class (the publisher) to notify other classes (the subscribers) when something happens. Events are built on top of delegates and provide a simplified way of implementing the Observer pattern in C#.

  • When to Use C# Events?

    C# events are best suited for situations where you need a lightweight, built-in way to implement publisher-subscriber behavior. They are ideal when:

    • You have a clear publisher-subscriber relationship.
    • You don’t need complex observer management (e.g., dynamic adding/removing of observers or complex interactions).
    • The observers do not require complex state management or coordination.
    • The communication pattern is synchronous.
  • Example in C#:

  public class WeatherStation
  {
      // Define the event using a delegate
      public event Action<int> TemperatureChanged;

      private int temperature;

      public int Temperature
      {
          get => temperature;
          set
          {
              temperature = value;
              // Raise the event when the temperature changes
              TemperatureChanged?.Invoke(temperature);
          }
      }
  }

  public class PhoneDisplay
  {
      public void OnTemperatureChanged(int newTemperature)
      {
          Console.WriteLine($"Phone Display: Temperature is {newTemperature}°C");
      }
  }

  public class Program
  {
      static void Main()
      {
          var weatherStation = new WeatherStation();
          var phoneDisplay = new PhoneDisplay();

          // Subscribe to the event
          weatherStation.TemperatureChanged += phoneDisplay.OnTemperatureChanged;

          weatherStation.Temperature = 25;  // This will notify the phone display
      }
  }
Enter fullscreen mode Exit fullscreen mode

Observer Pattern:

  • What is the Observer Pattern?

    The Observer pattern is a design pattern that defines a one-to-many relationship between objects, where a subject (or observable) maintains a list of its dependents (observers) and notifies them of state changes. The pattern is more flexible and can handle complex scenarios beyond what C# events typically handle.

  • When to Use the Observer Pattern?

    The Observer pattern is more appropriate when:

    • You need more control over the observer management (e.g., adding/removing observers dynamically).
    • You require more flexibility in terms of how observers react to changes (e.g., observers might pull data rather than being pushed updates).
    • You need to decouple the subject and observers more than what is offered by events.
    • You want to control the notification mechanism more finely, such as supporting asynchronous updates or managing observer state more explicitly.
  • Example in C#:

  public interface IObserver
  {
      void Update(int temperature);
  }

  public class WeatherStation
  {
      private List<IObserver> observers = new List<IObserver>();
      private int temperature;

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

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

      public int Temperature
      {
          get => temperature;
          set
          {
              temperature = value;
              NotifyObservers();
          }
      }

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

  public class PhoneDisplay : IObserver
  {
      public void Update(int temperature)
      {
          Console.WriteLine($"Phone Display: Temperature is {temperature}°C");
      }
  }

  public class Program
  {
      static void Main()
      {
          var weatherStation = new WeatherStation();
          var phoneDisplay = new PhoneDisplay();

          weatherStation.AddObserver(phoneDisplay);
          weatherStation.Temperature = 25;  // This will notify the phone display
      }
  }
Enter fullscreen mode Exit fullscreen mode

2. Detailed Comparison

Ease of Use:

  • C# Events: Events are straightforward to implement in C# due to language support. They offer a simple mechanism for publisher-subscriber relationships with minimal boilerplate code.
  • Observer Pattern: Implementing the Observer pattern requires more effort as you need to manually manage the list of observers, add/remove them, and implement the notification logic.

Flexibility:

  • C# Events: While simple, events are limited in terms of flexibility. All subscribers receive the same notification and cannot easily pull data or alter the notification flow.
  • Observer Pattern: The Observer pattern offers greater flexibility. You can customize how observers are notified, manage different types of observers, and implement more complex behaviors like filtering, prioritization, or different communication strategies (e.g., synchronous vs. asynchronous).

Decoupling:

  • C# Events: Events still couple the publisher to the delegate signature, making the relationship less flexible. If you need to change how the notification works, you may have to modify the event signature, impacting subscribers.
  • Observer Pattern: The Observer pattern allows for better decoupling, as the subject only needs to know about the observer interface, not specific implementations. This leads to more modular and easily extendable code.

Threading and Asynchronous Handling:

  • C# Events: Handling events in a multi-threaded environment can be challenging. You may need to manage thread safety manually, and events are typically synchronous unless explicitly managed.
  • Observer Pattern: The Observer pattern can be designed to handle asynchronous updates and multi-threading more naturally. For example, you could notify observers using tasks or other asynchronous mechanisms.

Use Cases:

  • C# Events: Suitable for scenarios where the publisher-subscriber relationship is simple and there isn’t a need for complex observer management. For example, event-driven components within a web API where the publisher and subscriber are tightly related and performance is a concern.
  • Observer Pattern: More appropriate for scenarios where you need complex observer management or need to decouple the subject from its observers. This is common in larger systems where components need to be modular, and the relationships between them are dynamic.

3. When to Use Each

C# Events:

  • Real-Time Notifications: If you need to trigger simple real-time notifications, such as logging, metrics collection, or sending notifications when certain actions happen (e.g., when a user logs in), C# events are a good choice.
  • Simplicity: When you need to keep things simple and are not dealing with many different subscribers or complex relationships between objects.

Observer Pattern:

  • Extensibility: When building a more complex system where components need to be easily extendable and decoupled, such as a plugin-based architecture or a system with many different types of observers (e.g., different modules listening for domain events).
  • Complex Subscriber Management: When you need to manage the lifecycle of subscribers dynamically, including adding, removing, and customizing their behavior.
  • Asynchronous Notifications: When you need to handle notifications in an asynchronous or distributed manner, such as sending updates to multiple microservices or handling webhooks.

4. Best Practices

  • Favor Simplicity: Use C# events when the requirements are simple. Avoid over-engineering the solution if a built-in mechanism like events suffices.
  • Modularize with Observer Pattern: When working on larger systems, consider using the Observer pattern to keep components modular and decoupled. This is especially important when your system needs to evolve over time or if you anticipate needing to add new observers in the future.
  • Combine Approaches: In some cases, you might find it beneficial to combine both approaches. For instance, you can use C# events for simple notifications and the Observer pattern for more complex scenarios that require dynamic observer management.

Conclusion

As a C# developer, your choice between C# events and the Observer pattern depends on the complexity of your system and the specific requirements at hand. If you need a simple, built-in mechanism for publisher-subscriber communication, C# events are usually sufficient. However, if you require more flexibility, decoupling, and extensibility, the Observer pattern is a better choice. In large, professional systems where components evolve and interact dynamically, the Observer pattern often proves more robust and scalable.

By understanding the trade-offs and use cases for each, you can make informed decisions that lead to better-structured and maintainable software systems.

Top comments (0)