使用流畅的构建器模式,没有内部静态类用于工作流 [英] Using a fluent builder pattern without inner static classes for workflow

查看:256
本文介绍了使用流畅的构建器模式,没有内部静态类用于工作流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从我在传递中所做的工作的延续1到许多相同对象类型的参数



我已经得到了很好的反馈,相信我有改进的设计。整个代码位于 https://github.com/spakai/flow_input_builder



要求很简单: -



我需要使用以前工作流程中的1个或更多输出为不同的工作流构建一组输入

我有一组接口

  public interface SwfInput {

}

public interface SwfOutput {

}

public interface工作流程<我扩展SwfInput,O扩展SwfOutput> {

public O execute(I input);
}

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

}

现在,说我有3个顺序执行的流FlowA> FlowB-> FlowC



FlowC需要来自FlowB的强制输出,但只可以从FlowA



有一个FlowCBuilder的实现

  public class FlowCInputBuilder实现Builder< FlowCInput> {

private final FlowBOutput mandatoryflowBOutput;
private FlowAOutput optionalflowAOutput;

public FlowAOutput getOptionalflowAOutput(){
return optionalflowAOutput;
}

public FlowCInputBuilder setOptionalflowAOutput(FlowAOutput optionalflowAOutput){
this.optionalflowAOutput = optionalflowAOutput;
返回这个;
}

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());
}

返回输入;

}


}

一个测试我已经写了一个示例使用

  FlowBOutput mandatoryflowBOutput = new FlowBOutput(); 

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

我没有为Builder模式使用静态内部类。



欢迎任何建议。

解决方案

您应该使用静态内部类。使用这种方法的关键是,内部可以直接访问正在构造的对象的私有属性。这有助于消除重复的代码,因为构建器不需要为构建维护一长串临时状态。所以,你的代码可以这样重写:

  public class FlowCInput {
private int output1FromB; //假设它是int
private String output2FromA; //假设它是String
private FlowCInput(){}
// ...

public static class FlowCInputBuilder implements Builder< FlowCInput> {
private final FlowCInput result;

public FlowCInputBuilder(FlowBOutput mandatoryflowBOutput){
result = new FlowCInput();
// output1FromB是私有的,但仍然从这里访问
result.output1FromB = mandatoryflowBOutput.getOutput1FromB();
}

public FlowCInputBuilder setOptionalflowAOutput(FlowAOutput optionalflowAOutput){
// same for output2FromA
result.output2FromA = optionalflowAOutput.getOutput2FromA();
返回这个;
}

@Override
public FlowCInput build(){
return result;
}
}
}

如你所见,现在只持有一个 FlowCInput 对象,它不会不必要地保持 mandatoryflowBOutput optionalflowAOutput 如前所述。


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 https://github.com/spakai/flow_input_builder

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.

解决方案

You should use static inner class. The key point of using this approach is that, the inner can directly access private properties of the object being constructed. This helps eliminating duplicated code since the builder does not need to maintain a long list of temporary state for the constructing. So, your code can be rewritten like this:

public class FlowCInput {
    private int output1FromB; // suppose that it is int
    private String output2FromA; // suppose that it is String
    private FlowCInput() { }
    //...

    public static class FlowCInputBuilder implements Builder<FlowCInput> {
        private final FlowCInput result;

        public FlowCInputBuilder(FlowBOutput mandatoryflowBOutput) {
            result = new FlowCInput();
            // output1FromB is private but still accessed from here
            result.output1FromB = mandatoryflowBOutput.getOutput1FromB();
        }

        public FlowCInputBuilder setOptionalflowAOutput(FlowAOutput optionalflowAOutput) {
            // same for output2FromA
            result.output2FromA = optionalflowAOutput.getOutput2FromA();
            return this;
        }

        @Override
        public FlowCInput build() {
            return result;
        }
    }
}

As you see, the builder now holds only a FlowCInput object, it does not unnecessarily hold mandatoryflowBOutput and optionalflowAOutput as before.

这篇关于使用流畅的构建器模式,没有内部静态类用于工作流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆