管道设计模式的实现 [英] Pipeline design pattern implementation
问题描述
这是关于管道实施的设计问题.以下是我的幼稚实现.
This is a design question regarding the implementation of a Pipeline. The following is my naive implementation.
管道中各个步骤/阶段的接口:
Interface for individual steps/stages in the pipeline:
public interface Step<T, U> {
public U execute(T input);
}
管道中步骤/阶段的具体实现:
Concrete implementations of steps/stages in pipeline:
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;
}
}
管道类将保存/注册管道中的步骤,并一个接一个地执行它们:
The pipeline class will hold/register the steps in the pipeline and execute them one after the other:
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;
}
}
}
用于执行管道的潜水程序:
Diver program to execute the pipeline:
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();
}
}
但是,正如您所看到的,朴素的实现有很多限制.
However, as you can see the naive implementation has many limitations.
主要要求之一是,由于要求每个步骤的输出可以是任何类型,因此朴素的实现不是类型安全的(Pipeline类中的execute方法).如果我碰巧错误地连接了管道中的步骤,则该应用程序将失败.
One of the major ones is that since the requirement is that the output of each step could be of any type, the naive implementation is not type-safe (the execute method in the Pipeline class). If I happen to wire the steps in the pipeline incorrectly, the app will fail.
任何人都可以通过添加已编码的内容来帮助我设计解决方案,或者将我指向已经存在的模式来解决此问题吗?
Can anyone help me design the solution by adding to what I have coded, or point me towards an already existing pattern to solve this?
推荐答案
我会专注于
如果我碰巧错误地连接了管道中的步骤,则该应用程序将失败.
If I happen to wire the steps in the pipeline incorrectly, the app will fail.
是的,这是一个问题. StepThree
是这里的陌生人.我认为没有一种简单的模式可能会有所帮助,但我确实认为它必须是策略和构建器模式的组合.例如:
Yes, this is a problem. StepThree
is the stranger here. I do not think one simple pattern might help, I do think it must be a combination of strategy and builder pattern. For example:
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;
}
}
使用快速生成器语法可能有效:
Using the fast-builder-syntax this might work:
Pipeline<String, Integer> pipe = new Pipeline<Integer, Integer>()
.add(new StepOne()).add(new StepTwo()).add(new StepThree());
这应该起作用,因为泛型不是字节码的一部分.
This should work since generics are not part of the bytecode.
这篇关于管道设计模式的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!