jeudi 11 mai 2017

Design Pattern Confusion

Hi community I am always one that is confused how and when to apply design patterns. Today my dilemma arises from trying to understand when to use and truly understanding the difference between a Strategy and Bridge Pattern.

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.

Aucun commentaire:

Enregistrer un commentaire