Java静态构建类似于IntStream

戴维斯:

我的目的是提供一个类来构建功能链,以在之后执行某些任务。到目前为止,这是我能想到的

功能类别

public class Loop {
    private final int from;
    private final int to;

    private Loop(int from, int to) {
        this.from = from;
        this.to = to;
    }

    public static Loop from(int from) {
        return new Loop(from, 0);
    }

    public static Loop to(int to) {
        return new Loop(0, to);
    }

    public void execute(Executable executable) {
        for (int i = from; i < to; i++) {
            executable.execute();
        }
    }
}

可执行接口

@FunctionalInterface
public interface Executable {
    void execute();
}

它可以用于一个参数(从或到),例如

Loop.to(10).execute(() -> {});

但我希望它适用于多个参数,例如

Loop.from(5).to(10).execute(() -> {});

我该如何实现?我也不确定带有一个冗余参数的static from和static to方法是否适合Loop类。

图灵85:

一个人可以采取几种方法。解决方案越复杂,它们通常变得越复杂。让我们从一个简单的解决方案开始。

public class Loop {
    private final int from;
    private final int to;

    private Loop(Builder builder) {
        this.from = builder.from();
        this.to = builder.to();
    }

    public static Builder from(int from) {
        return new Builder().from(from);
    }

    public static Builder to(int to) {
        return new Builder().to(to);
    }

    public void execute(Runnable executable) {
        for (int i = from; i < to; i++) {
            executable.run();
        }
    }

    public static class Builder {
        private int from = 0;
        private Integer to = null;

        private Builder() {}

        public Builder from(int from) {
            this.from = from;
            return this;
        }

        private int from() {
            return from;
        }

        public Builder to(int to) {
            this.to = to;
            return this;
        }

        private int to() {
            return to;
        }

        public void execute(Runnable runnable) {
            Objects.requireNonNull(runnable);
            new Loop(this).execute(runnable);
        }
    }
}

Ideone demo

这已经是相当了。但是我们可以例如打电话from(...)to(...)多次。如果只允许一个调用,from(...)而之后只允许调用to(...),那么我们需要定义更多类型。我们可以通过添加接口实现这一目标LoopFromSetBuilderLoopToSetBuilder并且LoopAllSetBuilder

interface LoopFromSetBuilder {
    LoopAllSetBuilder to(int to);
}

interface LoopToSetBuilder {
    LoopAllSetBuilder from(int from);
}

interface LoopAllSetBuilder {
    void execute(Runnable runnable);
}

加上一些方法上的调整 Loop

class Loop {
    ...
    public static LoopFromSetBuilder from(int from) {
        return new Builder().from(from);
    }

    public static LoopToSetBuilder to(int to) {
        return new Builder().to(to);
    }
    ...
}

Builder实现这些接口:

    public static class Builder implements LoopFromSetBuilder, LoopToSetBuilder, LoopAllSetBuilder {
        ...
    }

Ideone demo

我们elimiate为用户呼叫的可能性from(...),并to(...)多次。

我没有构建那些“复杂的”流畅API的进一步经验,但是我认为构建这样的API的过程可能会非常痛苦。对于小的例子,因为在这里,我们也许能够拒绝设置相同的变量复式倍,但似乎有更多的参数相当混乱(可能的状态-因此-接口似乎是在顺序2^n哪里n是字段数。 .. 嗯是的。

如果有人支持我的答案,那么也应该考虑提高Luk2302的答案,因为我的答案的第一个解决方案与Luk2302的解决方案非常相似,并且Luk2302的答案发布得比我的早一点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章