5

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.

freedev
  • 25,946
  • 8
  • 108
  • 125
Ankit Goel
  • 483
  • 1
  • 4
  • 11
  • 1
    Possible duplicate of [Strategy vs. Bridge Patterns](http://stackoverflow.com/questions/5863530/strategy-vs-bridge-patterns) – Lukasz_Plawny May 11 '17 at 08:52
  • 1
    http://stackoverflow.com/questions/5863530/strategy-vs-bridge-patterns, http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern – Andrew Tobilko May 11 '17 at 08:53
  • Strategy is behavior pattern and Bridge is structural pattern. Take a look to http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern – Saulius Next May 11 '17 at 08:53
  • http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern – Tobi May 11 '17 at 09:15

3 Answers3

3

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.

freedev
  • 25,946
  • 8
  • 108
  • 125
1

Strategy Pattern says that you can change the core algorithm later. For i.e. when you code eCommerce applications and you have B2B and B2C customers, that have different payment and ordering method. Then you would use the Strategy Pattern for the checkout strategy. So the basic action is the same, but the two users use different ways (strategies) to accomplish that. The bridge pattern in contrast says that you build a bridge between two modules and you can change these modules independently. This is targeting at abstraction and information hiding.

Stimpson Cat
  • 1,444
  • 19
  • 44
0

The bridge is used to encapsulate a component and to expose some other interface. You would typically use a bridge when you're using a component that has a life-cycle that you don't control. It limits the dependency of your software to the implementation of the bridge.

The strategy pattern is used when you have different possible algorithms to do a certain operation, and you wan't to keep the ability to switch from one to another.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24