The Bridge

Listen to this post
Subscribe

Intent

Separate a varying Entity from its varying Behavior, so that the two can vary independently. Another way to state the intent is, the Bridge is one variation using another variation in a varying way.

Example

One common use of the Bridge is in data access frameworks. The varying Entities would be elements of the system that needed to access data in different ways (such as sequentially, random access). The varying Behaviors would be the different data sources that actually store, retrieve, delete, and update data in different ways.

The idea is to be able to add new data consumers or new data providers without this effecting the other in each case. This makes the system open-closed to a new consumer, or a new provider, or both.

Diagram

Composite Pattern

Qualities and principles

Entities only consume, Behaviors only provide, yielding strong cohesion and separation of concerns.

Entities couple only to the Behavioral interface. Clients couple only to the Entity interface. Any potential redundancies can be pushed up to base classes.

Entities, Behaviors, and the way Entities use Behaviors are all hidden from clients. As a result, new Entities and new Behaviors can added to the system with little or no change to existing code.

Testing

Testing the Behaviors is straightforward as they have no dependencies. Testing an Entity requires a Mock Behavior.

Questions and concerns

Adding a new Behavior is usually straightforward, unless the new Behavior in question is lacking capability(s). If so, a Proxy or Decorator can be used to add functionality.

Adding a new Entity is also usually straightforward, unless the new Entity requires something of the Behaviors that is not currently in the interface. When this occurs the interface of the Behaviors must be changed, and thus all existing Behaviors must be updated as well.

The Bridge requires that all Behaviors have the same interface. If this is not true, then typically an Adapter or Façade is used to correct this.

More information

Visit the Net Objectives Pattern Repository: The Bridge Pattern

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.