SOLID & Design Patterns Interview Question #55

What is the Bridge Pattern?

Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate

Quick Interview Answer

The Bridge Pattern separates an abstraction from its implementation so both can vary independently. Instead of creating a large inheritance hierarchy for every combination, the abstraction contains or receives an implementation object.

Detailed Explanation

The Bridge Pattern separates an abstraction from its implementation so both can vary independently. Instead of creating a large inheritance hierarchy for every combination, the abstraction contains or receives an implementation object.

Bridge is useful when two dimensions of a design vary independently, such as notification type and delivery provider.

Code Example

public interface IMessageSender
{
    Task SendAsync(string message);
}

public abstract class Notification
{
    protected readonly IMessageSender Sender;

    protected Notification(IMessageSender sender)
        => Sender = sender;
}