mardi 5 novembre 2019

Chain of responsibility : Evaluating an alternate implementation

Background

Below code-snippet is not the classic implementation of chain of Responsibility, but aims towards solving the same problem of 'Avoiding coupling between the sender and receiver of a request', probably with better separation of concerns. The 'Chain' can still 'grow' and 'shrink' without the caller being impacted.

//Controller
public Output handleRequest(Input input){
    Output output;
    for(Strategy strategy : strategyList){
       StrategyOutput strategyOutput = strategy.execute(input);
       //modify the input if needed based upon the emitted output.
    }
    return output;
}

//Strategy1
public Class Strategy1 implements Strategy{

}

//Strategy2
public Class Strategy2 implements Strategy{

}

As per my understanding, the separation of concerns is much cleaner here as compared to the classic chain of responsibility. Separations being :

  • Separate controller (Orchestrates the chain).
  • Separate set of strategies. A Strategy bothers just about itself, not having to bother about calling other strategies (which are supposedly 'chain links' in chain of responsibilities).

Question:

  • Does this approach has any major drawback as compared to the classic chain of responsibility ?

Aucun commentaire:

Enregistrer un commentaire