设计模式-行为型06-命令模式(Command)
发布日期:2021-05-10 03:41:17 浏览次数:16 分类:精选文章

本文共 4166 字,大约阅读时间需要 13 分钟。

���������������Command Pattern������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

���������������������������������Invoker���������������������������������������������������Executioner������������������������������������������������Command���������������������������������������������������������������������������������������������������������������

���������������������������

  • ������������Invoker���

    • ���������������������������������������
    • ���������������������Command������������������������������������������������
  • ���������������Command���

    • ���������������������������������
    • ������������������������������
    • ���������������������������������������������������
  • ������������Executioner���

    • ���������������������������������������������
    • ������������������������������������������������������������������������������������
  • ���������������������������

  • UI���������������������

    • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������

    • ������������������������������������������������������������������������������������������������������������������������������
  • ������������������������

    • ������������������������������������������������������������������������������������������������������������������������
  • ���������������������

  • ������������

    • ���������������������������������������������������������������������������������������������������������������������������������
  • ���������������

    • ������������ ������������������������������������������������������������������������������������������������������
  • ������������

    • ������������������������������������������������������������������������������������������������������������������������������
  • ������������������������

  • ������������

    • ������������Command���������������������������������������������������Accept������������������������������������������������������������������Execute���������
  • ������������������

    • ������������������������������������������������Command������������CommandImpl������������Command���������������Accept���Execute���������
  • ������������������

    • ���������������������Command������������������������������������������Execute������������������������������
  • ���������Java������������������

    ������������������������Java���������������������������������������

    // ������������
    public interface Command {
    void accept(String parameters);
    void execute();
    }
    // ���������������
    public class CommandImpl implements Command {
    private String command;
    public CommandImpl(String command) {
    this.command = command;
    }
    @Override
    public void accept(String parameters) {
    // ���������������������
    this.command = parameters;
    }
    @Override
    public void execute() {
    // ���������������������
    if (command.equals("������������")) {
    System.exit(0);
    } else {
    System.out.println("���������������" + command);
    }
    }
    }
    // ������������
    public class Invoker {
    private Command command;
    public void setCommand(Command cmd) {
    this.command = cmd;
    }
    public void invoke() {
    command.accept("������");
    command.execute();
    }
    }
    // Main������������������������������
    public class Main {
    public static void main(String args[]) {
    // ���������������������������������
    Command cmd = new CommandImpl("������������");
    Invoker invoker = new Invoker();
    invoker.setCommand(cmd);
    invoker.invoke();
    }
    }

    ������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:设计模式-行为型07-备忘录模式(Memento)
    下一篇:设计模式-行为型05-责任链模式(Chain of Responsibility)

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年04月02日 20时52分48秒