7

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

mirta
  • 644
  • 10
  • 25
  • 1
    Kudos for the nice diagram. You get a point for just that. But it will be very difficult to answer your question because you are asking us whether chain-of-responsibility is a good solution for your problem, but all we know about your problem is that it is a problem which, according to your description, requires a chain-of-responsibility solution. Also: programmers.stackexchange.com is more suitable than stackoverflow for questions of this kind. – Mike Nakis Nov 30 '15 at 21:14
  • If you don't insist on calling it chain of responsibilities you can simply implement it so that all handlers in the chain have to pass the list through until the end. – zapl Nov 30 '15 at 21:21
  • 1
    @MikeNakis when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Nov 30 '15 at 21:54
  • @gnat duh, of course. Will try to keep this in mind for the next time. – Mike Nakis Nov 30 '15 at 21:56

1 Answers1

1

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 ?

k0ner
  • 1,086
  • 7
  • 20