public class Receiver { public void action(){ System.out.println(Receiver.class); } } public interface Command { void execute(); } public class ConcreteCommand implements Command { private Receiver receiver = null; public ConcreteCommand(Receiver receiver) { this.receiver = receiver; } @Override public void execute() { receiver.action(); } } public class Invoker { private Command command = null; public Invoker(Command command) { this.command = command; } public void action() { command.execute(); } } // 將接受者的一個操做 --> 封裝爲一條命令 --> 傳遞給給執行操做人 public class Client { public static void main(String[] args) { Receiver receiver = new Receiver(); Command command = new ConcreteCommand(receiver); Invoker invoker = new Invoker(command); invoker.action(); } }
學習設計模式強烈推薦的博客:java_my_lifejava
代碼地址:lennongit