我使用命令设计模式来处理玩家的操作。
例如,下面是处理掷骰子的命令。
例如,下面是处理掷骰子的命令。
interface ICommand
{
public function execute(Game $game) : void;
}
class RollDiceCommand implements ICommand
{
private $player;
public function __construct(Player $player)
{
$this->player = $player;
}
public function execute(Game $game) : void
{
$dice = DiceFacade::roll(new NumberGenerator());
// Currently a business logic goes here
if ($dice->isDouble()) {
$player->incrementDoubleCount();
if ($player->getDoubleCount() === 3) {
$command = new GoToJailCommand();
$command->execute();
return;
}
} else {
// The next player turn
$game->nextPlayer();
}
$command = MoveForwardCommand($this->player);
$command->execute($dice->getValue());
// ...
}
}
- 在命令中存储额外的业务逻辑是个好主意吗?
- 我应该直接从roll命令调用另一个命令,还是需要避免这样做?在命令中抛出事件的想法对我来说似乎更好。您对此有什么看法?
谢谢!