mardi 11 octobre 2016

Using a fluent builder pattern without inner static classes for workflow

This is a continuation from what I was working in Passing 1 to many parameters of same object type

I've gotten good feedback on that , I believe i have the improved the design . The whole code is at http://ift.tt/2dJLnht

The requirement is simple : -

I need to build a set of input for different workflows using 1 or more outputs from previous workflows

I have a set of interfaces

public interface SwfInput {

}

public interface SwfOutput {

}

public interface Workflow<I extends SwfInput, O extends SwfOutput> {

    public O execute(I input);
}

public interface Builder<I extends SwfInput> {
    public I build();

}

Now , Say I have 3 flows which gets executed in sequence FlowA->FlowB->FlowC

FlowC needs mandatory output from FlowB but only optionally from FlowA

so I have a implementation for FlowCBuilder

public class FlowCInputBuilder implements Builder<FlowCInput> {

    private final FlowBOutput mandatoryflowBOutput;
    private FlowAOutput optionalflowAOutput;

    public FlowAOutput getOptionalflowAOutput() {
        return optionalflowAOutput;
    }

    public FlowCInputBuilder setOptionalflowAOutput(FlowAOutput optionalflowAOutput) {
        this.optionalflowAOutput = optionalflowAOutput;
        return this;
    }

    public FlowCInputBuilder(FlowBOutput mandatoryflowBOutput) {
        this.mandatoryflowBOutput = mandatoryflowBOutput;
    }

    @Override
    public FlowCInput build() {

        FlowCInput input = new FlowCInput();
        input.setMandatoryFromFlowB(mandatoryflowBOutput.getOutput1FromB());

        if (optionalflowAOutput != null) {
            input.setOptionalFromFlowA(optionalflowAOutput.getOutput2FromA());
        }

        return input;       

    }


}

one test i have written shows an example usage

 FlowBOutput mandatoryflowBOutput = new FlowBOutput();

 mandatoryflowBOutput.setOutput1FromB("iNeedThis");
 FlowAOutput optionalflowAOutput = new FlowAOutput();
 FlowCInput input =  new FlowCInputBuilder(mandatoryflowBOutput)
                .setOptionalflowAOutput(optionalflowAOutput)
                .build();

I have not used static inner class for the Builder pattern.

Any suggestions are welcomed.

Aucun commentaire:

Enregistrer un commentaire