管道设计模式实现 [英] Pipeline design pattern implementation

查看:37
本文介绍了管道设计模式实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于流水线实现的设计问题.以下是我的幼稚实现.

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 程序:

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

Pipeline 是这样的:

Whereat Pipeline is like this:

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 这可能有效:

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屋!

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