59 lines
1.1 KiB
Java
59 lines
1.1 KiB
Java
package cn.bunny.pattern5.demo3;
|
|
|
|
import lombok.Setter;
|
|
|
|
public class CommandDemo {
|
|
public static void main(String[] args) {
|
|
Receiver receiver = new Receiver();
|
|
|
|
ConcreteCommand concreteCommand = new ConcreteCommand(receiver);
|
|
|
|
Invoker invoker = new Invoker();
|
|
invoker.setCommand(concreteCommand);
|
|
invoker.invoke();
|
|
}
|
|
|
|
// 命令接口
|
|
interface Command {
|
|
|
|
/**
|
|
* 执行方法
|
|
*/
|
|
void execute();
|
|
}
|
|
|
|
// 接受者
|
|
static class Receiver {
|
|
public void action() {
|
|
System.out.println("接受者 执行。。。");
|
|
}
|
|
}
|
|
|
|
// 具体的执行命令
|
|
static class ConcreteCommand implements Command {
|
|
|
|
private final Receiver receiver;
|
|
|
|
public ConcreteCommand(Receiver receiver) {
|
|
this.receiver = receiver;
|
|
}
|
|
|
|
/**
|
|
* 执行方法
|
|
*/
|
|
@Override
|
|
public void execute() {
|
|
receiver.action();
|
|
}
|
|
}
|
|
|
|
@Setter
|
|
static class Invoker {
|
|
private Command command;
|
|
|
|
public void invoke() {
|
|
command.execute();
|
|
}
|
|
}
|
|
}
|