如何使用可运行的版本将Java命令模式迁移到PHP 7.4?

问题描述 投票:1回答:1

出于研究目的,我正在尝试将此Java命令模式示例迁移到PHP:

https://codereview.stackexchange.com/questions/52110/command-pattern-implementation

[As @simon commented,使用方法引用运算符,将使代码相当现代化:

class MyCommand implements Order {
    private final Runnable action;

    public MyCommand(Runnable action) {
         this.action = action;
    }

    @Override
    public void execute() {
         action.run();
    }
}

然后您可以创建如下命令:

MyCommand bsc = new MyCommand(stock::buy);
MyCommand ssc = new MyCommand(stock::sell);

我当前的PHP实现在这里:https://3v4l.org/iIHn9

那么实现PHP中的MyCommand类的最佳方法是什么?

java php runnable porting command-pattern
1个回答
0
投票

这里有一些有关PHP设计模式的参考:

© www.soinside.com 2019 - 2024. All rights reserved.