Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

public interface Action { void execute(); } public class OpenFileAction implements…

  1. User

    public interface Action { void execute(); }

    public class OpenFileAction implements Action { private File file;

    public OpenFileAction(File file) { this.file = file; }

    @Override public void execute() { file.open(); } }

    public interface File { void open(); }

    public class Document implements File { @Override public void open() { System.out.println("Document opened."); } }

    public class FileOpener { private Action action;

    public void setAction(Action action) { this.action = action; }

    public void performAction() { action.execute(); } }

    public class Client { public static void main(String[] args) { File document = new Document(); Action openAction = new OpenFileAction(document); FileOpener opener = new FileOpener(); opener.setAction(openAction); opener.performAction(); } } 命令模式的代码模板

  2. Assistant

    你给出的代码很典型地展示了**命令模式(Command Pattern)**的实现模板。下面是对代码结构和命令模式核心要素的详细说明,以及对应的模板说明:


    命令模式基本结构与代码模板讲解

    1. 命令接口(Command Interface)

    定义命令的统一接口,声明执行操作的方法。

    public interface Action {
       void execute();
    }
    

    2. 具体命令类(Concrete Command)

    实现命令接口,绑定了一个接收者对象以及调用该接收者方法的具体实现。

    public class OpenFileAction implements Action {
       private File file;
    
       public OpenFileAction(File file) {
           this.file = file;
       }
    
       @Override
       public void execute() {
           file.open();
       }
    }
    

    3. 接收者(Receiver)

    命令真正的执行者,封装具体操作,如打开文件。

    public interface File {
       void open();
    }
    
    public class Document implements File {
       @Override
       public void open() {
           System.out.println("Document opened.");
       }
    }
    

    4. 调用者(Invoker)

    持有命令对象,并调用命令执行方法。

    public class FileOpener {
       private Action action;
    
       public void setAction(Action action) {
           this.action = action;
       }
    
       public void performAction() {
           action.execute();
       }
    }
    

    5. 客户端(Client)

    创建具体命令对象并将接收者绑定到命令上,随后把命令对象传给调用者。

    public class Client {
       public static void main(String[] args) {
           File document = new Document();
           Action openAction = new OpenFileAction(document);
           FileOpener opener = new FileOpener();
           opener.setAction(openAction);
           opener.performAction();
       }
    }
    

    命令模式的代码模板总结

    // 命令接口 (Command)
    public interface Command {
        void execute();
    }
    
    // 具体命令类 (Concrete Command)
    public class ConcreteCommand implements Command {
        private Receiver receiver;
    
        public ConcreteCommand(Receiver receiver) {
            this.receiver = receiver;
        }
    
        @Override
        public void execute() {
            receiver.action();
        }
    }
    
    // 接收者 (Receiver)
    public class Receiver {
        public void action() {
            // 实际业务逻辑,比如打开文件、关闭文件等
        }
    }
    
    // 调用者 (Invoker)
    public class Invoker {
        private Command command;
    
        public void setCommand(Command command) {
            this.command = command;
        }
    
        public void invoke() {
            command.execute();
        }
    }
    
    // 客户端 (Client)
    public class Client {
        public static void main(String[] args) {
            Receiver receiver = new Receiver();
            Command command = new ConcreteCommand(receiver);
            Invoker invoker = new Invoker();
            invoker.setCommand(command);
            invoker.invoke();
        }
    }
    

    小结:

    • 命令模式通过封装请求为对象,实现请求调用者和接收者之间的解耦。
    • 方便增加新的命令,不影响调用者和接收者。
    • 可以实现请求的排队、日志、撤销操作等功能。

    你给的代码非常符合这一模式,已经体现出完整的命令模式结构。