Design Pattern: Bridge vs Strategy
Context
Last week, while digging around composition design in software development, I found a question that many people asked about design pattern:.
What is the difference between the bridge pattern and the strategy pattern?
Although these two design patterns are different, their UMLs are similar, the implementation are also similar. Some people even say they are the same thing, just being used for different purpose.
In this post, I will explain the definitions of Bridge and Strategy with some examples, and the differences between them.
Strategy
The strategy pattern is a Behavioral pattern
Gang of Four definition:
“Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it”
This pattern contains one abstract strategy interface and many concrete strategy implementations (algorithms) of that interface. The application uses strategy interface only. Depending in some configuration parameter, the concrete strategy will be tagged to interface.
When to use Strategy:
- You have more ways (multiple algorithms) for doing an operation
- You want to choose the algorithm at run-time (how your object behave)
- You want to modify a single Strategy without a lot of side-effects at compile-time
Image from refactoring.guru
A lot of resources on the Internet use the word “context” when explaining strategy pattern. Simply put, “context” is the class contains the operation (method) Context tied to the Strategy: The “context” would know/contain the strategy interface reference and the implementation to invoke the strategy behavior on it.
Example:
I want to build a navigation application where user can find route from one point to another. And user can choose which way they want to take (by train, by car, on foot etc)
Here we have:
- Context is the navigation app
- Operation is finding a route
- Multiple algorithms are ways to navigate
- Choosing/switching algorithms at run-time: user can choose which method to navigate
The UML Diagram for the sample will look like this:
Sample code:
|
|
Result:
|
|
Bridge
The bridge pattern is a Structural pattern
Gang of Four definition:
“Decouple an abstraction from its implementation so that the two can vary independently” Let’s analyze the definition: we have “abstraction” and “implementation”
- Abstraction: a high-level interface that contains non-specific implementation of any control logic (or business logic)
- Implementation (or “logic”): responsible for the low-level work, separated from abstraction
When to use Bridge:
- You have many implementations
- Implementation and abstraction are developed independently/separated
- Abstraction doesn’t know the implementation
- Client can only access to abstraction, any changes made to implementation should not affect client
Image from refactoring.guru
Example:
We will continue with our navigation application above. Now the user can choose the route, and we need to implement the feature. There will be some constraints:
- There will be 2 platforms: smartphone and smartwatch. The application will have different behavior due to differences in hardware (sensor etc)
- Depend on the manufacturer, Google service availability is different. Our application has to:
- use Google map and Google direction service on Google service supported device
- use OpenStreetMap and Open Route Service on Google service non-supported device
We will also assume that user doesn’t care which service we use, as long as they can get to where they want.
Here we have:
- Abstraction is the navigator
- Implementation are Google service and OpenStreetMap service
The UML Diagram for the sample will look like this:
Sample code:
|
|
Result:
|
|
The difference between Bridge and Strategy
We will look at the two UMLs again: Bridge will completely hide the concrete implementation from the client, while in strategy, the client can “inject” the implementation. Bridge pattern is mostly used where you have many classes that are orthogonal with each other (sometimes called two dimensions in the domain)
|
|
References:
GeeksforGeeks - Bridge Design Pattern