【设计模式】—— 命令模式Commond

简介:

  模式意图

  将一个请求封装成一个对象,从而对这个命令执行撤销、重做等操作。

  典型的Eclipse开发中,编辑器的操作就需要用到这个模式,比如Undo、Redo等等。

  另外这个模式使得一个命令的触发与接收解耦,这样我们就可以演变成把感兴趣的对象接收这个命令,当命令触发时,这些对象就会执行操作。这个机制也是java事件的处理方式。

  应用场景

  1 命令抽象成对象

  2 在不同的时刻,指定或者排队命令

  3 支持 Undo或者Redo等操作

  4 修改日志,当系统崩溃时,利用修改日志执行撤销

  5 原语操作上构造一个高层系统(不理解)

  模式结构

  Invoker 命令的触发者,触发一个命令的执行。

复制代码
/**
 * 命令的触发者,发送命令
 * @author xingoo
 *
 */
class Invoker{
    private Commond commond;
    
    public Invoker(Commond commond) {
        this.commond = commond;
    }
    
    public void action(){
        commond.excute();
    }
}
复制代码

  Receiver 命令的接受者,针对命令,执行一定的操作。

复制代码
/**
 * 命令的接受者,负责接收命令,进行处理
 * @author xingoo
 *
 */
class Receiver{
    
    public Receiver() {
        
    }
    
    public void action(){
        System.out.println("Action of receiver!");
    }
}
复制代码

  Commond 命令的抽象接口

复制代码
/**
 * 命令接口,定义命令的统一接口
 * @author xingoo
 *
 */
interface Commond{
    public void excute();
}
复制代码

  ConcreteCommond 具体的命令,关联一个接收者对象,当命令执行时,执行这个接收者对应的操作。

复制代码
/**
 * 具体的命令
 * @author xingoo
 *
 */
class ConcreteCommond implements Commond{
    
    private Receiver receiver;
    
    public ConcreteCommond(Receiver receiver) {
        this.receiver = receiver;
    }
    
    public void excute() {
        receiver.action();
    }
    
}
复制代码

  全部代码

复制代码
 1 package com.xingoo.Commond;
 2 /**
 3  * 命令的触发者,发送命令
 4  * @author xingoo
 5  *
 6  */
 7 class Invoker{
 8     private Commond commond;
 9     
10     public Invoker(Commond commond) {
11         this.commond = commond;
12     }
13     
14     public void action(){
15         commond.excute();
16     }
17 }
18 /**
19  * 命令的接受者,负责接收命令,进行处理
20  * @author xingoo
21  *
22  */
23 class Receiver{
24     
25     public Receiver() {
26         
27     }
28     
29     public void action(){
30         System.out.println("Action of receiver!");
31     }
32 }
33 /**
34  * 命令接口,定义命令的统一接口
35  * @author xingoo
36  *
37  */
38 interface Commond{
39     public void excute();
40 }
41 /**
42  * 具体的命令
43  * @author xingoo
44  *
45  */
46 class ConcreteCommond implements Commond{
47     
48     private Receiver receiver;
49     
50     public ConcreteCommond(Receiver receiver) {
51         this.receiver = receiver;
52     }
53     
54     public void excute() {
55         receiver.action();
56     }
57     
58 }
59 /**
60  * 客户端调用者
61  * @author xingoo
62  *
63  */
64 public class Client {
65     public static void main(String[] args) {
66         Receiver receiver = new Receiver();
67         Commond commond = new ConcreteCommond(receiver);
68         System.out.println("Commond register in here!");
69         
70         try {
71             Thread.sleep(3000);
72         } catch (InterruptedException e) {
73             // TODO Auto-generated catch block
74             e.printStackTrace();
75         }
76         
77         System.out.println("Commond excute in here!");
78         Invoker invoker = new Invoker(commond);
79         invoker.action();
80     }
81 }
复制代码

  运行结果

Commond register in here!
Commond excute in here!
Action of receiver!

 

本文转自博客园xingoo的博客,原文链接:【设计模式】—— 命令模式Commond,如需转载请自行联系原博主。
相关文章
|
5月前
|
设计模式 UED
二十三种设计模式:解锁软件开发的神器:命令模式与撤销重做功能
二十三种设计模式:解锁软件开发的神器:命令模式与撤销重做功能
130 0
|
8天前
|
设计模式 存储 算法
Java设计模式-命令模式(16)
Java设计模式-命令模式(16)
|
2月前
|
设计模式 存储 Java
【十二】设计模式~~~行为型模式~~~命令模式(Java)
文章详细介绍了命令模式(Command Pattern),这是一种对象行为型模式,用于将请求封装成对象,实现请求发送者与接收者的解耦,从而降低系统耦合度、提高灵活性,并支持命令的排队、记录、撤销和恢复操作。通过案例分析、结构图、时序图和代码示例,文章展示了命令模式的组成部分、实现方式和应用场景,并讨论了其优点、缺点和适用情况。
|
3月前
|
设计模式 JavaScript API
js设计模式【详解】—— 命令模式
js设计模式【详解】—— 命令模式
35 6
|
4月前
|
设计模式
命令模式-大话设计模式
命令模式-大话设计模式
|
4月前
|
设计模式 Java uml
必知的技术知识:JAVA【设计模式】命令模式
必知的技术知识:JAVA【设计模式】命令模式
23 0
|
4月前
|
设计模式 Java
Java设计模式之命令模式详解
Java设计模式之命令模式详解
|
4月前
|
设计模式
设计模式之命令模式
设计模式之命令模式
|
5月前
|
设计模式 Java 调度
【设计模式系列笔记】命令模式
命令模式是一种行为设计模式,它将请求封装成一个对象,从而允许使用不同的请求、队列或日志请求参数化客户端对象,同时支持撤销操作。
57 1
|
5月前
|
设计模式 存储 Java
【搞懂设计模式】命令模式:从遥控器到编程的妙用!
【搞懂设计模式】命令模式:从遥控器到编程的妙用!
62 0