设计模式-命令行模式

前言

命令行模式在Hystrix源码中是一个比较典型的案例!

实例代码

public interface Command {
    void execute();
}

public class CommandImpl implements Command{

    private Receiver receiver;

    public CommandImpl(Receiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void execute() {
        receiver.action();
    }
}

public class CommandTestMain {

    public static void main(String[] args) {
        Receiver receiver = new Receiver();
        Command command = new CommandImpl(receiver);
        Invoker invoker = new Invoker();
        invoker.setCommand(command);
        invoker.execute();
    }

}

public class Invoker {

    private Command command;

    public void setCommand(Command command) {
        this.command = command;
    }

    public void execute() {
        command.execute();
    }
}

public class InvokerAndReceiver {
    /**
     * 命令的抽象
     */
    interface Command {
        void execute();
    }

    /**
     * 调用者
     */
    static class Invoker {
        private Command command;

        public void setCommand(Command command) {
            this.command = command;
        }

        public void execute() {
            command.execute();
        }
    }

    /**
     * 业务的具体实现
     */
    static class Receiver {
        public void action() {
            //do some task.
            System.out.println("doing...");
        }
    }

    /**
     * 命令的封装
     */
    static class CommandImpl implements Command {
        private Receiver receiver;
        public CommandImpl(Receiver receiver) {
            this.receiver = receiver;
        }
        @Override
        public void execute() {
            receiver.action();
        }
    }

    public static void main(String[] args) {
        Receiver receiver=new Receiver();
        Command command=new CommandImpl(receiver);
        Invoker invoker=new Invoker();
        invoker.setCommand(command);
        invoker.execute();
    }
}

public class Receiver {

    public void action() {
        //do some task.
        System.out.println("doing...");
    }
}

目前只是简单记录后期完善

你可能感兴趣的:(#,设计模式,设计模式,java,android)