GofPatterns Gofpatterns

Structural Patterns  «Prev  Next»

Bridge Pattern

Bridge Pattern Description
Divide a complex component into 2 separate but related inheritance hierarchies.
  1. Refined Abstraction
  2. Implementor is an interface that inherits the 2 classes Concrete Implementor A,B
Bridge Pattern
Bridge Pattern Code

When To Use:
You should use the Bridge pattern when:
  1. You want to avoid a permanent binding between an abstraction and its implementation
  2. Both the abstractions and their implementations should be extensible using subclasses.
  3. Changes in the implementation of an abstraction should have no impact on clients; that is, you should not have to recompile their code.
Benefits of the Bridge Pattern:
  1. Enables you to separate the interface from the implementation
  2. Improves extensibility
  3. Hides implementation details from clients

The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that
  1. a) decoupling and
  2. b) abstraction can vary independently.
The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.
When a class varies often, the features of object-oriented programming become very useful because changes to a program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful when both the class as well as what it does vary often.
The 1) class itself can be thought of as the implementation and 2) what the class can do as the abstraction. The bridge pattern can also be thought of as two layers of abstraction.