流水线设计模式实现

Pipeline design pattern implementation

这是一个关于管道实现的设计问题。以下是我天真的实现。

管道中个人 steps/stages 的接口:

public interface Step<T, U> {
    public U execute(T input);
}

管道中 steps/stages 的具体实现:

public class StepOne implements Step<Integer, Integer> {
    @Override
    public Integer execute(Integer input) {
        return input + 100;
    }
}

public class StepTwo implements Step<Integer, Integer> {
    @Override
    public Integer execute(Integer input) {
        return input + 500;
    }
}

public class StepThree implements Step<Integer, String> {
    @Override
    public String execute(Integer input) {
        return "The final amount is " + input;
    }
}

流水线class会hold/register流水线中的步骤,一个接一个地执行:

public class Pipeline {
    private List<Step> pipelineSteps = new ArrayList<>();
    private Object firstStepInput = 100;

    public void addStep(Step step) {
        pipelineSteps.add(step);
    }

    public void execute() {
        for (Step step : pipelineSteps) {
            Object out = step.execute(firstStepInput);
            firstStepInput = out;
        }
   }
}

执行管道的驱动程序:

public class Main {
    public static void main(String[] args) {
        Pipeline pipeline = new Pipeline();
        pipeline.addStep(new StepOne());
        pipeline.addStep(new StepTwo());
        pipeline.addStep(new StepThree());

        pipeline.execute();
    } 
}

但是,如您所见,简单的实现有很多局限性。

其中一个主要问题是,由于要求每个步骤的输出可以是任何类型,因此天真的实现不是类型安全的(Pipeline class 中的 execute 方法)。如果我碰巧错误地连接了管道中的步骤,应用程序将失败。

任何人都可以通过添加到我编码的内容来帮助我设计解决方案,或者指出我已经存在的模式来解决这个问题吗?

我会专注于

If I happen to wire the steps in the pipeline incorrectly, the app will fail.

是的,这是一个问题。 StepThree是这里的陌生人。我不认为一个简单的模式可能有帮助,我确实认为它必须是策略和构建器模式的组合。例如:

Pipeline<Integer,Integer> intPipe = new Pipeline<>();
intPipe = intPipe.add(new StepOne()); // increment 100
intPipe = intPipe.add(new StepTwo()); // increment 500
Pipeline<String, Integer> strPipe = intPipe.add(new StepThree()); // convert

哪里的管道是这样的:

public static class Pipeline<IN, OUT> {
   //...
   public<A> Pipeline<OUT,A> add(Step<IN,A> step) {
     pipelineSteps.add(step);
     return (Pipeline<OUT,A>)this;
   }
}

使用 fast-builder-syntax 这可能有效:

Pipeline<String, Integer> pipe = new Pipeline<Integer, Integer>()
    .add(new StepOne()).add(new StepTwo()).add(new StepThree());

这应该可行,因为泛型不是字节码的一部分。

你的方法很不错。但是,我会像这样编写管道 class:

public class Pipeline {
    private List<Step> pipelineSteps = new ArrayList<>();
    private Object firstStepInput = 100;

    public Pipeline() {
        pipelineSteps.add(new StepOne());
        pipelineSteps.add(new StepTwo());
        pipelineSteps.add(new StepThree());
    }

    public void execute() {
        for (Step step : pipelineSteps) {
            Object out = step.execute(firstStepInput);
            firstStepInput = out;
        }
    }

    public String getResult() {
        return (String) firstStepInput;
    }
}

这样一来,所有的具体步骤知识都封装在了Pipeline中class。

在这种情况下,execute方法可以执行一个循环。但是,如有必要,execute class 可以逐个执行这些步骤。

为什么需要额外的 Pipeline class?我想你可以去掉中间人。这将使您的 api 更简单,例如:

Step<Integer, String> source = Step.of(Object::toString);
Step<Integer, Integer> toHex = source.pipe(it -> Integer.parseInt(it, 16));

toHex.execute(11/*0x11*/);// return 17;

您可以在 中简单地实现管道模式,如下所示:

interface Step<I, O> {

    O execute(I value);

    default <R> Step<I, R> pipe(Step<O, R> source) {
        return value -> source.execute(execute(value));
    }

    static <I, O> Step<I, O> of(Step<I, O> source) {
        return source;
    }
}

在之前的 java 版本中,您可以使用摘要 class 代替:

abstract static class Step<I, O> {

    public abstract O execute(I value);

    public <R> Step<I, R> pipe(Step<O, R> source) {
        return new Step<I, R>() {
            @Override
            public R execute(I value) {
                return source.execute(Step.this.execute(value));
            }
        };
    }

    public static <I, O> Step<I, O> of(Step<I, O> source) {
        return source;
    }
}

基本可以用chain of responsibility design pattern

public class Pipeline {

    private List<Step> pipelineSteps = new ArrayList<>();
    private Object firstStepInput = 100;

    public Pipeline() {
        pipelineSteps.add(new StepOne());
        pipelineSteps.add(new StepTwo());
        pipelineSteps.add(new StepThree());
}

您不需要为此创建新界面。

Java 8 已经有一个名为 Function 的函数式接口,它允许您创建一个函数链(换句话说,您的管道)。

Function<Integer, Integer> addOne = it -> {
            System.out.println(it + 1);
            return it + 1;
        };

Function<Integer, Integer> addTwo = it -> {
            System.out.println(it + 2);
            return it + 2;
        };

Function<Integer, Integer> timesTwo = input -> {
            System.out.println(input * 2);
            return input * 2;
        };

final Function<Integer, Integer> pipe = addOne
        .andThen(timesTwo)
        .andThen(addTwo);

pipe.apply(10);

如果您想阅读更多关于函数式接口的内容:https://medium.com/@julio.falbo/java-recent-history-java-8-part-2-functional-interface-predefined-functional-interface-2494f25610d5