vendredi 16 août 2019

Chain of responsibility pattern vs Iterating over a list of strategies

How does a CoR pattern compare to implementing the items in the chain as a list and having a single orchestrator class try the items in this list sequentially?

Which is a better approach?

Example:

Class A implements NodeInAChain{

NodeInAChain nextNode;

public Result doWork(Request request){
// Some logic
if(logicSuccessful) {
 return;
else
 nextNode.doWork(request);
}
}

Vs

Class ChainOrchestrator {

List<NodeInAChain> nodeList;

public Result doWork(Request request){
foreach(node : nodeList) {
 if(node.doWork(request) == 'success'){
   break;
 } 
}
return result;
}

`

Aucun commentaire:

Enregistrer un commentaire