Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern - Strategy and Bridge (Overlap in design)

Tags:

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.

like image 564
Ankit Goel Avatar asked May 11 '17 08:05

Ankit Goel


People also ask

What is difference between Bridge and strategy pattern?

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.

What is the purpose of the design pattern Bridge?

The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern.

What is Bridge in 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.

What is the Bridge pattern example?

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.


1 Answers

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.

like image 57
freedev Avatar answered Sep 22 '22 10:09

freedev