命令模式

来源:互联网 发布:goodnotes mac破解版 编辑:程序博客网 时间:2024/05/18 20:36

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

/** * 真正的执行者 *  * @author zhangjianbin *  */public class Receiver {    public void action() {        System.err.println("真正的执行者执行命令");    }}
/** * 命令的接口 *  * @author zhangjianbin *  */public interface Command {    /**     * 执行命令     */    void execute();}class ConcreateCommand implements Command {    /**     * 命令的真正执行者     */    private Receiver receiver;    public ConcreateCommand(Receiver receiver) {        super();        this.receiver = receiver;    }    @Override    public void execute() {        //执行命令前或后可以做一些复杂的处理,如记录志        // 执行命令        receiver.action();    }}
/** *  * 调用者或发起者 *  * @author zhangjianbin *  */public class Invoke {    // 可以多条命令,也可以是一条命令    private Command command;    public Invoke(Command command) {        super();        this.command = command;    }    /**     * 作用:用于调用执行该命令的执行者     */    public void call() {        // 执行命令前或执行命令后可以做一些复杂的处理        command.execute();    }}
public class Client {    public static void main(String[] args) {        //命令对象        Command cmd = new ConcreateCommand(new Receiver());        //真正的调用者,也就是命令的发起者        Invoke invoke = new Invoke(cmd);        invoke.call();    }}
0 0
原创粉丝点击