企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## 1. 是什么是命令行设计模式 ## 2. 如何使用命令行设计模式 ## 3. 代码怎么实现 public interface Command { public void execute(); } public class Light { public void on(){ System.out.println("light is on"); } public void off(){ System.out.println("light is off"); } } public class LightCommand implements Command{ Light light; public LightCommand(Light light){ this.light = light; } @Override public void execute() { light.on(); } } public class SimpleRemote { Command slot; public SimpleRemote(){} public void setCommand(Command command){ slot =command; } public void buttonWasPressed(){ slot.execute(); } } public class RemoteController{ public static void main(String[] args) { SimpleRemote remote = new SimpleRemote(); Light light = new Light(); LightCommand lightCommand= new LightCommand(light); remote.setCommand(lightCommand); remote.buttonWasPressed(); } }