命令模式git
public interface ICommand { void execute(); }
public class Receiver { public void execute(){ System.out.println("receiver execute ... "); } }
public class ConcreteCommandA implements ICommand { private Receiver receiver; public ConcreteCommandA(Receiver receiver){ this.receiver = receiver; } @Override public void execute() { System.out.println("ConcreteCommandA execute ..."); receiver.execute(); } } public class ConcreteCommandB implements ICommand { private Receiver receiver; public ConcreteCommandB(Receiver receiver){ this.receiver = receiver; } @Override public void execute() { System.out.println("ConcreteCommandB execute ..."); receiver.execute(); } }
public class Invoker { private ICommand concreteCommandA, concreteCommandB; public Invoker(ICommand concreteCommandA, ICommand concreteCommandB){ this.concreteCommandA = concreteCommandA; this.concreteCommandB = concreteCommandB; } public void orderA(){ concreteCommandA.execute(); } public void orderB(){ concreteCommandB.execute(); } }
public static void main(String[] args) { Receiver receiver = new Receiver(); Invoker invoker = new Invoker(new ConcreteCommandA(receiver), new ConcreteCommandB(receiver)); invoker.orderA(); invoker.orderB(); }
ConcreteCommandA execute ... receiver execute ... ConcreteCommandB execute ... receiver execute ...
https://github.com/Seasons20/DisignPattern.git
ENDgithub