1 of 8

Bridge Design Pattern �(Structural)

Thanh Le – Software Architect / Technical Consultant

Complexity: 3/3

Popularity: 1/3

2 of 8

Intent

  • Bridge is a structural design pattern that lets you split a larger class or set of closely related classes into two separated hierarchies (abstraction and implementation) – which can be developed independently of each other.

��

Image source: internet

3 of 8

Motivation

You have a geometric Shape class with a pair of subclasses: Circle and Square.

You want to extend this class hierarchy to incorporate colors, so you plan to create Red and Blue shape subclasses. However, since you already have two subclasses, you’ll need to create four class combinations such as BlueCircle and RedSquare.

���

Image source: internet

Number of class combinations grows in geometric progression.

4 of 8

Solution

  • You can apply bridge design pattern.

  • The Bridge pattern attempts to solve this problem by switching from inheritance to the object composition. It means that you extract one of the dimensions into a separate class hierarchy, so that the original classes will reference an object of the new hierarchy, instead of having all of its state and behaviors within one class.

Image source: internet

You can prevent the explosion of a class hierarchy by transforming it into several related hierarchies.

5 of 8

Abstraction and Implementation

  • In the Bridge pattern, we want to separate the high-level functionality of a system (abstraction) from the low-level implementation details (implementation). This allows us to change the implementation without affecting the abstraction, or vice versa.

  • In a software system, we want to define a high-level interface (abstraction) that clients can use to access functionality, without worrying about the implementation details. By separating the abstraction and implementation, we can change the implementation without affecting the abstraction, or change the abstraction without affecting the implementation. �

Image source: internet

6 of 8

Structure

Image source: internet

  • Abstraction provides high-level control logic. It relies on the implementation object to do the actual low-level work.

  • Implementation declares the interface that’s common for all concrete implementations. An abstraction can only communicate with an implementation object via methods that are declared here.

  • Concreate Implementations – contain platform-specific code.

  • Refined Abstractions – provide variants of control logic. Like their parent, they work with different implementations via the general implementation interface

7 of 8

Pros/Cons

  • Single Responsibility Principle: You can focus on high-level logic in the abstraction and on platform details in the implementation
  • Open/Closed Principle: you can introduce new implementations and abstractions independently from each other.
  • The client code works with high-level abstractions. It isn’t exposed to the platform details
  • You might make the code more complicated by applying the pattern to highly cohesive class

8 of 8

Practice

https://github.com/thanhle0212/23GoF-Design-Patterns-CSharp