Design Patterns in C# Part 4 - Enhancing Event-Driven Architectures With The Observer Pattern - by João Melo - Medium
Design Patterns in C# Part 4 - Enhancing Event-Driven Architectures With The Observer Pattern - by João Melo - Medium
Open in app
Search
Introduction
In our previous posts, we explored powerful design patterns like the Strategy
Pattern, Factory Method Pattern, and Decorator Pattern. Today, we delve into the
Observer Pattern, which enhances event-driven architectures by allowing objects
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-4-enhancing-event-driven-architectures-with-the-observer-pattern-33632811964e 1/14
19/12/2024, 16:04 Design Patterns in C# Part 4: Enhancing Event-Driven Architectures with the Observer Pattern | by João Melo | Medium
💡Type — Behavioral patterns are concerned with algorithms and the assignment
of responsibilities between objects. They help manage and organize complex
communications between entities in a system.
The Observer Pattern is a behavioral design pattern where an object, known as the
subject, maintains a list of dependents, called observers, and notifies them of any
state changes, usually by calling one of their methods. This pattern establishes a
one-to-many relationship between a subject and its observers, allowing multiple
objects to listen and react to changes in another object.
Use the Observer Pattern when you need to establish a dependency between objects
in a way where changes to one object require updating multiple others without
coupling the objects tightly together. It’s ideal for scenarios where decoupling is
important and when objects need to be notified of changes in another object’s state.
Imagine you are developing a stock market monitoring application where users
want to track the prices of specific stocks in real-time. Instead of polling for updates
continuously, you can use the Observer Pattern. Each stock (subject) maintains a list
of investors (observers) interested in its price changes. When the price of a stock
changes, all registered investors are notified automatically, allowing them to update
their own displays or take action based on the new price.
The IStock interface defines the contract for a stock object that observers can
subscribe to. It includes methods for attaching and detaching observers, notifying
observers of changes ( Notify() ), and accessing the stock's symbol and price.
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-4-enhancing-event-driven-architectures-with-the-observer-pattern-33632811964e 2/14
19/12/2024, 16:04 Design Patterns in C# Part 4: Enhancing Event-Driven Architectures with the Observer Pattern | by João Melo | Medium
The Stock class implements the IStock interface and represents a specific stock
with a symbol and price. It maintains a list of registered investors (observers) and
provides methods to attach, detach, and notify observers when its price changes.
Changes in price trigger notifications to all subscribed investors.
return _symbol;
}
The IInvestor interface defines the contract for observers interested in monitoring
stock prices. It requires an Update() method that subjects can call to notify
observers of changes in the stock's state.
The Investor class implements the IInvestor interface and represents an investor
interested in tracking stock prices. Each investor has a name and responds to price
change notifications by displaying a message detailing the stock's symbol and its
new price.
In the Main method of the Program class, we create instances of Stock and
Investor . We attach investors to the stock object to observe price changes
( Attach() ), simulate price updates ( SetPrice() ), and detach investors when they no
longer wish to receive notifications ( Detach() ). This demonstrates how the Observer
Pattern allows dynamic updates and notifications between subjects and observers.
class Program
{
static void Main(string[] args)
{
Stock microsoft = new Stock("MSFT", 350.00);
IInvestor investor1 = new Investor("Investor 1");
IInvestor investor2 = new Investor("Investor 2");
microsoft.Attach(investor1);
microsoft.Attach(investor2);
microsoft.SetPrice(360.00);
microsoft.SetPrice(370.00);
microsoft.Detach(investor2);
microsoft.SetPrice(380.00);
}
}
Conclusion
In this post, we’ve explored the Observer Pattern, which facilitates event-driven
architectures by allowing objects to subscribe and react to changes in another
object’s state. This pattern promotes loose coupling between objects, making it
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-4-enhancing-event-driven-architectures-with-the-observer-pattern-33632811964e 5/14
19/12/2024, 16:04 Design Patterns in C# Part 4: Enhancing Event-Driven Architectures with the Observer Pattern | by João Melo | Medium
easier to maintain and extend applications. We demonstrated its use with a stock
market monitoring example, where investors can track real-time changes in stock
prices.
Stay tuned for our next post in this series, where we’ll explore another design
pattern, the Singleton Pattern, and see how it can further improve your C#
development skills!
👁️
Follow
Software engineer, specializing in backend development with the C# language and the .NET Core.
https://medium.com/@joaopedrosmelo/design-patterns-in-c-part-4-enhancing-event-driven-architectures-with-the-observer-pattern-33632811964e 6/14