BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Bridge Design Pattern

The bridge pattern is a design pattern that separates the abstract elements of a class from its technical implementation. This provides a cleaner implementation of real-world objects and allows the implementation details to be changed easily.

What is the Bridge Pattern?

The bridge pattern is a Gang of Four design pattern. This is a structural pattern as it defines a manner for creating relationships between classes or entities. The bridge pattern is used to separate the abstract elements of a class from the implementation details. For example, the abstract elements may be the business logic of an application. They can be created without any knowledge of the implementation details of their data access or interoperability with the operating system. The pattern provides the means to replace the implementation details without modifying the abstraction. This permits, for example, changing operating systems, databases, etc. with no impact to the business logic.

When the bridge pattern is not used, you may find that implementation details are included within the same classes as abstract elements. The way in which implementation details are changed is then generally be through inheritance, with subclasses providing different implementations. This can be problematic when refined abstractions are included, also through inheritance. The number of required classes can grow exponentially as new classes that contain both business logic and implementation details are added to a system. When there is a single implementation for a single abstraction, only one class would be required. However, if you have four abstractions combined with five implementations, this can potentially require twenty classes with lots of overlap. When using the bridge design pattern, far fewer classes are necessary for all but the simplest scenarios. This is due to the pattern removing platform dependencies from the abstraction.

Another benefit of the bridge pattern is that it introduces the possibility of changing the implementation details at run-time. This could permit the user to switch implementations to determine how the software interoperates with other systems. For example, allowing the user to decide whether to store information in a database, XML file or using another storage mechanism.

An example of the bridge pattern could be used within a system that sends messages, perhaps describing system status changes, warnings and errors. An abstraction class that represents a message could include properties to hold the details of the message. Several implementations could then be included to permit sending the messages via email, over a messaging queue or to a web service. The implementation to be used could then be selected according to the availability of these resources.

Implementing the Bridge Pattern

Bridge Design Pattern UML

The UML class diagram above describes an implementation of the bridge design pattern. The items in the diagram are described below:

  • Abstraction. This class contains members that define an abstract business object and its functionality. It also acts as the base class for other, more refined, abstractions. Objects of this type hold a reference to the particular implementation that they are using for platform-specific functions. No other member of the class deals with implementation details.
  • RefinedAbstraction. Many refined abstractions may inherit from the Abstraction class. Each provides a more specific variation upon the abstraction but still contains no implementation details other that those in the Implementation object reference that they hold.
  • ImplementationBase. This abstract class is the base class for all classes that provide implementation details for the associated abstractions. The class provides a fixed interface that can be utilised by the abstractions. The interface need not be similar to that of the abstraction. This class may be implemented as an interface if it provides no real functionality for its subclasses.
  • ConcreteImplementation. The ConcreteImplementation class inherits form the ImplementationBase class. There may be multiple concrete implementation classes, each providing the same interface but providing platform-specific functionality.

The following shows the basic code of the bridge design pattern implemented using C#. For brevity, the Abstraction class's Implementer property is declared using C# 3.0 automatically implemented property syntax. For earlier versions of the .NET framework, define the property in full and use the protected scope for the backing variable so that it is also available to refined abstractions.

public class Abstraction
{
    public ImplementationBase Implementer { get; set; }

    public virtual void Operation()
    {
        Console.WriteLine("ImplementationBase:Operation()");
        Implementer.OperationImplementation();
    }
}


public class RefinedAbstraction : Abstraction
{
    public override void Operation()
    {
        Console.WriteLine("RefinedAbstraction:Operation()");
        Implementer.OperationImplementation();
    }
}


public abstract class ImplementationBase
{
    public abstract void OperationImplementation();
}


public class ConcreteImplementation1 : ImplementationBase
{
    public override void OperationImplementation()
    {
        Console.WriteLine("ConcreteImplementation1:OperationImplementation()");
    }
}


public class ConcreteImplementation2 : ImplementationBase
{
    public override void OperationImplementation()
    {
        Console.WriteLine("ConcreteImplementation2:OperationImplementation()");
    }
}
8 April 2009