设计模式/行为型设计模式
简述
把一个请求或者操作封装到一个对象中以便使用不同的请求、队列或者日志来参数化其他对象
UML图
 
代码
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 
 | @Slf4j
 public class Receiver {
 public void receive() {
 log.debug(“接受者”);
 }
 }
 
 
 public interface CatCommand {
 void command();
 }
 
 
 @Slf4j
 public class DefaultCommand implements CatCommand {
 private Receiver receiver;
 
 public DefaultCommand(Receiver receiver) {
 this.receiver = receiver;
 }
 
 @Override
 public void command() {
 log.debug("默认命令 -- ");
 receiver.receive();
 }
 }
 
 @Slf4j
 public class Invoker {
 private CatCommand catCommand;
 
 public Invoker(CatCommand catCommand) {
 this.catCommand = catCommand;
 }
 
 public void invoke() {
 log.debug(“执行者”);
 catCommand.command();
 }
 }
 
 
 public class Client {
 public static void main(String[] args) {
 Receiver receiver = new Receiver();
 CatCommand catCommand = new DefaultCommand(receiver);
 Invoker invoker = new Invoker(catCommand);
 invoker.invoke();
 }
 }
 
 |