Bridge | Cheat Sheet

Structural Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readJun 16, 2021

--

A Bridge decouples an abstraction from its implementation, so that you can change the layer independently. Also, objects can change their implementation at runtime, and this leads to results in high dynamics and flexibility at runtime. This pattern is a good example for complying with the Open-Closed-Principle.

The Bridge pattern is incorporated into the system design from the very beginning. The Adapter pattern, on the other hand, is often applied after the system design is complete to allow unrelated classes to work together (resolving incompatibilities).

Real-life examples

  • Sending a message (abstraction) with different kinds of services like SMS, email, or messenger (implementation)
  • Using a remote control (abstraction) for different devices like radio or TV (implementation)
  • Enterprise application GUI (abstraction) running on different operating systems like Windows, MacOS, or Linux (implementation)

Meaning

  • Decoupling an abstraction from its implementation
  • Independent variation of abstraction and implementation

Applicability (suitable if …)

  • Permanent binding between abstraction and implementation is supposed to be prevented (allows runtime-conditioned changes of the implementation)
  • Abstraction and implementation are supposed to be extended by subclasses
  • Modifications to the abstraction should have no effect on the client
  • Numerical class growth occurs — Raumbaugh calls such class hierarchies “nested generalizations”
  • Implementation is supposed to be shared by several objects (e.g. reference counting)

Assets and Drawbacks

  • Decoupling of interface and implementation: objects can change their implementation at runtime, resulting in high dynamics and flexibility at runtime
  • Favors layering, which favors system structuring
  • Better extensibility, since abstraction hierarchies and implementor hierarchies can be extended independently of each other
  • Hides the implementation details from the client

Example

For this example we create a remote control (abstraction) for different devices: radio and TV (implementation),

First of all, we need an interface for the Implementor — in this case the device. Additionally, we implement the concrete classes.

Implementor
Concrete Implementor

After this, we need an implementation for the Abstraction — the remote control.

Abstraction
Concrete Abstraction

The client can create different types of devices (implementation) for one remote control (abstraction) and can control them by a GUI layer.

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Structural Patterns are indispensable when objects are combined to create new functionalities. Comprehensive structures are built by using composition of classes and objects. This provides flexibility due to variable composition structure depending on the runtime. One thus achieves an increase in efficiency and consistency.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links