vendredi 21 décembre 2018

Difference Between State and Strategy Design Pattern

I know it has been asked before but there is no clear answer to it , every one just explains each pattern without telling an actual difference

Is there any actual difference beside the intent and that a state cab preform a state transition in the state pattern

    interface IA{
      void A1();
    }

    class SubA1 : IA
    {
      public void A1()  {}
    }

    class SubA2 : IA
    {
      public void A1() {  }
    }

    class SubA3  : IA
    {
      public void A1() {  }
    }

public class Context{

  private IA _stateOrAction;

// i could be injecting a strategy algorithm or setting initial state 
  public Context(IA stateOrAction)
  {
    _stateOrAction= stateOrAction;
  }

// i could be changing a strategy algorithm or chaining current state    
  public void Set_IA(IA stateOrAction)
  {
    _stateOrAction= stateOrAction;
  }

  public void DoWorkDependingOnStateOrAlgorithm(object iCouldBeHereOrNot)
  {
    // just ignore that this is not same signature as interface
    _stateOrAction.A1(iCouldBeHereOrNot);
  }

}

the code above taken from a github repo which explained one of the two patterns ,
after putting a generic method and class name which hides the intent like this , is it still possible to till the difference , specially if we removed the Set_IA

Aucun commentaire:

Enregistrer un commentaire