通用receiver类
/**
* 通用receiver类
*/
public abstract class Receiver {
public abstract void doSomething();
}
具体receiver类
/**
* 具体receiver类
*/
public class ConcreteReciver1 extends Receiver {
@Override
public void doSomething() {
}
}
抽象的Command类
/**
* 抽象的Command类
*/
public abstract class Command {
public abstract void execute();
}
具体的command类
/**
* 具体的command类
*/
public class ConcreteCommand1 extends Command {
private Receiver receiver;
public ConcreteCommand1(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
this.receiver.doSomething();
}
}
调用者Invoker类
/**
* 调用者Invoker类
*/
public class Invoker {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void action(){
this.command.execute();
}
}
public class Client {
public static void main(String[] args) {
Invoker invoker = new Invoker();
Receiver receiver = new ConcreteReciver1();
Command command = new ConcreteCommand1(receiver);
invoker.setCommand(command);
invoker.action();
}
}
来源:CSDN
作者:取个名字有点烦
链接:https://blog.csdn.net/zxl0428/article/details/104030891