Observer Design Pattern in ASP.NET Core

Ibrahim Jaber
3 min readOct 21, 2023

Greetings, software enthusiasts! Today, we embark on a journey into the realm of the Observer design pattern in ASP.NET Core, approaching this topic from a professional standpoint. In this guide, tailored for individuals with a serious interest in software design, we will delve into the Observer pattern through the lens of a real-world business case. By the end of this read, you will gain a profound understanding of this elegant pattern and its practical applications.

The Professional Scenario: Financial Market Data

Consider a scenario where you are a senior software architect at a prominent financial institution, “CapitalWatch.” This firm provides real-time market data to a wide array of clients, including traders, analysts, and investors. The challenge you face is to ensure that when market data changes, all subscribed clients receive instantaneous updates, allowing them to make informed decisions.

The Observer Pattern: A Fundamental Concept

The Observer pattern is a behavioral design pattern that establishes a one-to-many relationship between objects. This pattern enables an object (the subject) to notify its dependents (observers) of any state changes, ensuring that they remain synchronized without creating tight coupling. Let’s delve into how this pattern can address the real-time data dissemination challenge at CapitalWatch.

Step 1: Create the Subject — MarketDataFeed

In our professional scenario, the MarketDataFeed serves as the subject. This component continuously receives real-time market data and is responsible for notifying all subscribing clients when significant market changes occur.

public class MarketDataFeed
{
private decimal _currentStockPrice;

public event EventHandler<decimal> MarketDataChanged;

public decimal CurrentStockPrice
{
get { return _currentStockPrice; }
set
{
_currentStockPrice = value;
OnMarketDataChanged(_currentStockPrice);
}
}

protected virtual void OnMarketDataChanged(decimal newPrice)
{
MarketDataChanged?.Invoke(this, newPrice);
}
}

Step 2: Create Observers — The Clients

In the financial world of CapitalWatch, various clients actively monitor market data to make informed decisions. These clients are the observers, and they register with the MarketDataFeed to receive real-time updates.

public class Client
{
public string Name { get; }

public Client(string name)
{
Name = name;
}

public void ReceiveMarketData(decimal stockPrice)
{
Console.WriteLine($"{Name} received a market data update: Stock price is now ${stockPrice}");
}
}

Step 3: Register Observers

To ensure that clients receive real-time updates, the financial software at CapitalWatch facilitates the registration of clients with the MarketDataFeed. This registration establishes the link between the subject and the observers.

MarketDataFeed marketData = new MarketDataFeed();
Client clientA = new Client("Client A");
Client clientB = new Client("Client B");

marketData.MarketDataChanged += clientA.ReceiveMarketData;
marketData.MarketDataChanged += clientB.ReceiveMarketData;

Subscribing to the MarketDataChanged event ensures that Client A and Client B will receive immediate notifications whenever the market data changes.

Step 4: Broadcasting Market Updates

As the senior software architect, you play a pivotal role in ensuring that when significant market changes occur, you can broadcast updates. This ensures that both Client A and Client B receive the most recent market data promptly.

marketData.CurrentStockPrice = 150.25;

Upon executing this code, both Client A and Client B will promptly receive market updates:

Client A received a market data update: Stock price is now $150.25
Client B received a market data update: Stock price is now $150.25

In Conclusion

The Observer design pattern is a vital tool in software development, providing a means to establish a loosely coupled one-to-many relationship between objects. In the context of CapitalWatch, it ensures that all subscribing clients receive real-time market data updates, enabling them to make well-informed financial decisions.

As you navigate the intricacies of software design and architecture in your professional endeavors, embrace the power of the Observer pattern. It enables the creation of highly flexible, maintainable, and efficient systems. May your code always be as precise and up-to-date as the real-time market data at CapitalWatch! 📈💼🖥️

--

--

Ibrahim Jaber

Software developer | Programming and Blockchain enthusiast | If you'd like to buy me coffee: https://ko-fi.com/ibrahimjaber