Chain of Responsibility design pattern implemented in Java.

This design pattern makes it easy to process some data (perform some operations on it) in specified order.

Without this pattern, this could have been written like this:

Data data = new Data(5, 2);

Processor processor1 = new Add5ToAProcessor();
Processor processor2 = new IfAGreaterThanBAdd5ToBProcessor();
Processor processor3 = new Add8ToBProcessor();

processor1.process(data);
processor2.process(data);
processor3.process(data);

//data was processed

With pattern it can be done like this:

Data data = new Data(5, 2);

Processor processor = new Add5ToAProcessor(new IfAGreaterThanBAdd5ToBProcessor(new Add8ToBProcessor(null)));
processor.process(data);

//data was processed

Of course you can add as many processors as you want, and all you have to do is call processor.process(data) only once.

It is also worth noting, that this pattern works really well with Template Method pattern, as shown in Processor.process(Data) and Processor.logic() in example below.

Main.java

package chain;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Main {
    public static void main(String[] args) {
        Data data = new Data(5, 2);

        Processor processor = new Add5ToAProcessor(new IfAGreaterThanBAdd5ToBProcessor(new Add8ToBProcessor(null)));
        processor.process(data);

        System.out.println(data);
    }
}

After running the output should be:

Adding 5 to A
Adding 5 to B
Adding 8 to B
Data{a=10.0, b=15.0}

Data.java

package chain;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Data {
    private double a;
    private double b;

    public Data(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public void addToA(double value){
        a += value;
    }

    public void addToB(double value){
        b += value;
    }

    public boolean isAGreaterThanB(){
        return a > b;
    }

    @Override
    public String toString() {
        return "Data{" +
                "a=" + a +
                ", b=" + b +
                '}';
    }
}

Processor.java

package chain;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public abstract class Processor {
    protected Processor nextProcessor;

    public Processor(Processor nextProcessor) {
        this.nextProcessor = nextProcessor;
    }

    public void process(Data data){
        logic(data);

        if(nextProcessor != null){
            nextProcessor.process(data);
        }
    }

    public abstract void logic(Data data);
}

Add5ToAProcessor.java

package chain;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Add5ToAProcessor extends Processor {

    public Add5ToAProcessor(Processor nextProcessor) {
        super(nextProcessor);
    }

    @Override
    public void logic(Data data) {
        System.out.println("Adding 5 to A");
        data.addToA(5);
    }
}

Add8ToBProcessor.java

package chain;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Add8ToBProcessor extends Processor {
    public Add8ToBProcessor(Processor nextProcessor) {
        super(nextProcessor);
    }

    @Override
    public void logic(Data data) {
        System.out.println("Adding 8 to B");
        data.addToB(8);
    }
}

IfAGreaterThanBAdd5ToBProcessor.java

package chain;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class IfAGreaterThanBAdd5ToBProcessor extends Processor {

    public IfAGreaterThanBAdd5ToBProcessor(Processor nextProcessor) {
        super(nextProcessor);
    }

    @Override
    public void logic(Data data) {
        if(data.isAGreaterThanB()){
            System.out.println("Adding 5 to B");
            data.addToB(5);
        }
    }
}

 



Java Chain of Responsibility
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close