Tutorials Design Patterns in C#

Publish-Subscribe Pattern — Complete Guide

Publish-Subscribe Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.

On this page

Design Patterns in C# · Lesson 44 of 69

Publish-Subscribe Pattern

GoF Core ✓Enterprise ✓Cloud & Craft

Cloud & Craft · 3 — Microservices & interviews · ~6 min · Module 5: Modern Enterprise Patterns

What is this?

Pub/Sub delivers messages from publishers to many subscribers via a broker or in-process bus — subscribers do not know each other.

Why should you care?

ShopNest order_placed fans out to inventory, email, and analytics independently.

See it live — copy this example

Paste into a C# console or class library project and run dotnet run.

public sealed class InProcBus
{
    private readonly Dictionary<string, List<Action<string>>> _subs = new();
    public void Subscribe(string topic, Action<string> handler)
    {
        if (!_subs.TryGetValue(topic, out var list)) _subs[topic] = list = new();
        list.Add(handler);
    }
    public void Publish(string topic, string message)
    {
        if (_subs.TryGetValue(topic, out var list))
            foreach (var h in list) h(message);
    }
}

var bus = new InProcBus();
bus.Subscribe("order_placed", m => Console.WriteLine($"email {m}"));
bus.Subscribe("order_placed", m => Console.WriteLine($"index {m}"));
bus.Publish("order_placed", "o-9");

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • One publish notifies all subscribers.
  • Production uses Rabbit/Kafka; the idea stays the same.
  • Failures in one subscriber should not erase others (isolate).

Practice next

  1. Publish and see two handlers.
  2. Add analytics subscriber.
  3. Move to a real broker for cross-process.
  4. Unsubscribe a handler.
  5. Topic per event type.

Remember

One-to-many messaging. Loose coupling. Broker for distribution.

ShopNest order_placed fan-out

Bus notifies email + search.

Outcome: Email outage does not block indexing if isolated.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Subject Interface (INewsPublisher):?
Short answer: This interface defines methods for subscribing, unsubscribing, and notifying observers. The subject manages a list of observers and notifies them when there is an update. public interface INewsPublisher Exa…
Mid PDF Detailed
Subject (NewsPublisher):?
Short answer: The NewsPublisher class acts as the subject in the Observer Pattern. It maintains a list of IObserver instances (subscribers) and provides methods to add (Subscribe), remove (Unsubscribe), and notify them (…
Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Mid PDF Detailed
Redo Functionality:?
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation. Real-world exampl…
Mid PDF Detailed
Abstract Expression (IExpression):?
Short answer: The IExpression interface declares an Interpret method, which is the core of the interpreter pattern. This method is used to interpret and evaluate expressions. public interface IExpression Example code { i…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details