Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain of Responsibility - pass the request through all the chains

I browsed the web but I couldn't find an answer to my question...

Lets say I have 3 chains. I want the request to pass all 3 chains (it doesn't matter if the chain can handle the request or not). Is it possible to use CoR pattern for this problem?

To explain it better - I have a list that has to pass through several sets of rules. If it passes the 1st rule, list stays the same. Then it goes on to the 2nd rule, and 2nd rule changes a list. The changed list goes to the 3rd rule, it passes and the altered list is saved. Chains

like image 946
mirta Avatar asked Nov 10 '22 00:11

mirta


1 Answers

Hm, I don't see any counter argument not to do that.

You can simply declare your Processor or however you call that:

abstract class Processor {
    private Processor successor;

    public void setSuccessor(Processor successor) { this.successor = successor; }

    public List process(List input) {
        List processed = this.internalProcess(input);
        if (successor != null) { return successor.process(processed); }
        return processed;
    }

    protected abstract List internalProcess(List input);

}

and then you can define for example:

public class ProcessorNoProcess extends Processor {

    @Override protected List internalProcess(List input) { 
       // do nothing
       return input;
    }
}

Is that what you were asking ?

like image 82
k0ner Avatar answered Nov 14 '22 22:11

k0ner