Java:使用 onComplete 回调简单实现命令模式?

发布于 2024-10-06 12:44:41 字数 60 浏览 3 评论 0原文

有人可以指出在哪里可以找到带有 onComplete 回调的命令模式的实现吗?例如可以在串行宏命令中使用?

Could someone point out where to find an implementation of the Command pattern with onComplete callbacks that could be used for example in a serial macro command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2024-10-13 12:44:41

尝试这个

abstract class Command {
    final public void execute(){
          run();
          onComplete();
    }
    protected abstract void run();
    protected abstract void onComplete();
}
//usage
abstract class HiCommand extends Command {
    protected void run(){
        System.out.println("Hi, ");
    }

    protected abstract void onComplete();
}

new HiCommand(){
    @Override
    protected void onComplete() {
        System.out.println("world");
    }
}.execute();

或这个

interface WhenDone{
    void onComplete();
}

abstract class Command {
    private final WhenDone callback;
    protected Command(WhenDone callback){
        this.callback = callback;
    }
    final public void execute(){
          run();
          callback.onComplete();
    }
    protected abstract void run();
}
//usage
class PrintHi extends Command {
    protected PrintHi(WhenDone callback){
        super(callback);
    }

    protected void run(){
        System.out.println("Hi, ");
    }
}

class PrintWorld implements WhenDone {
    public void onComplete(){
        System.out.println("world!");
    }
}
//somewhere
new PrintHi(new PrintWorld()).execute();

使用示例并非来自现实生活。也许,您应该通过创建 CommandManager 将创建和调用执行分开。

Try this

abstract class Command {
    final public void execute(){
          run();
          onComplete();
    }
    protected abstract void run();
    protected abstract void onComplete();
}
//usage
abstract class HiCommand extends Command {
    protected void run(){
        System.out.println("Hi, ");
    }

    protected abstract void onComplete();
}

new HiCommand(){
    @Override
    protected void onComplete() {
        System.out.println("world");
    }
}.execute();

or this

interface WhenDone{
    void onComplete();
}

abstract class Command {
    private final WhenDone callback;
    protected Command(WhenDone callback){
        this.callback = callback;
    }
    final public void execute(){
          run();
          callback.onComplete();
    }
    protected abstract void run();
}
//usage
class PrintHi extends Command {
    protected PrintHi(WhenDone callback){
        super(callback);
    }

    protected void run(){
        System.out.println("Hi, ");
    }
}

class PrintWorld implements WhenDone {
    public void onComplete(){
        System.out.println("world!");
    }
}
//somewhere
new PrintHi(new PrintWorld()).execute();

Examples of usage isn't from real life. Probably, you should separate creation and calling execution via creation CommandManager .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文