Today my dilemma arises from trying to understand why there is overlapping in how Strategy and Bridge Pattern can be implemented.
Here is Bridge Pattern (Abstracting an implementation from an abstraction)
// Shapes object structure will not be concerned how they draw themselves
public abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
// This could also be put in the subcla
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
Now here is Strategy Pattern - a class behavior or its algorithm can be changed at run time. A Calculator will delegate its operations to a strategy
public class Calculator{
private Strategy strategy;
public Calculator(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}
Both of these patterns involve discarding strategy objects which encapsulate functionality. Please help with a clear difference between the Bridge Pattern (Structural) and Strategy Pattern (Behavioral). Another confusion I am having is that they are under different umbrellas of knowledge.
Strategy Pattern is used for Behavioural decisions, while Bridge Pattern is used for Structural decisions. Brigde Pattern separats the abstract elements from the implementation details, while Strategy Pattern is concerned making algorithms more interchangeable.
The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern.
Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
The Bridge pattern decouples an abstraction from its implementation, so that the two can vary independently. A household switch controlling lights, ceiling fans, etc. is an example of the Bridge. The purpose of the switch is to turn a device on or off.
In your example I see a little of overlapping between these two patterns.
In the first case, Bridge pattern is used when we need to decouple an abstraction from its implementation so that the two can vary independently. That's all, you're just abstracting the implementation.
In the second case Strategy a class behaviour can be changed at run time. Which also means that into to a concrete Strategy class you could also have implemented a Bridge pattern, maybe you're using a class and want to decuple it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With