Void value as return parameter

后端 未结 8 2513
夕颜
夕颜 2021-02-19 18:50

I have this interface:

public interface Command {
    T execute(String... args);
}

it works fine for most uses. But when I try to mode

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 19:19

    What if instead of having an interface like:

    public interface Command {
        T execute(String... args);
    }
    

    You instead had:

    public interface Command {
        void execute(String... args);
        T getResult();
        bool hasResult();
    }
    

    Then callers would do:

    public void doSomething(Command cmd) {
        cmd.execute(args);
        if(cmd.hasResult()) {
            // ... do something with cmd.getResult() ...
        }
    }
    

    You could also create an interface VoidCommmand that extends Command, if you like.

    This seems like the cleanest solution to me.

提交回复
热议问题