zoukankan      html  css  js  c++  java
  • java23种设计模式-行为型模式-命令模式

    一、定义

    命令模式属于对象的行为模式。命令模式又称为行动(Action)模式或交易(Transaction)模式。命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

    二、优点及缺点

    优点:

    1、降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。

    2、增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。

    3、可以实现宏命令。

    4、方便实现Undo和 Redo 操作。命令模式可以与后面介绍的备忘录模式结合,实现命令的撤销与恢复。

    缺点:

    1、可能产生大量具体命令类。因为计对每一个具体操作都需要设计一个具体命令类,这将增加系统的复杂性。

    三、代码实现:

     提供类:

    package com.example.demo.sjms.minglingmoshi;
    
    import lombok.Data;
    
    /**
     *  @Author: caesar
     *  @Date:2020年11月27日 09:11:30
     *  @Description: 内容类(作为消息提供者存在)
     */
    @Data
    public class Content {
        private String name;
    }

    命令抽象类:

    package com.example.demo.sjms.minglingmoshi;
    
    import com.example.demo.sjms.celuemoshi.Context;
    
    /**
     *  @Author: caesar
     *  @Date:2020年11月27日 09:11:44
     *  @Description: 命令模式抽象类
     */
    public abstract class Command {
        protected Content context;
    
        /**
         *  @Author: caesar
         *  @Date:2020年11月27日 09:11:13
         *  @Description: 执行命令
         */
        public abstract void execute();
    }

    命令实现类:

    package com.example.demo.sjms.minglingmoshi;
    
    /**
     *  @Author: caesar
     *  @Date:2020年11月27日 10:11:46
     *  @Description: 第一个命令实现类
     */
    public class FirstCommand extends Command{
        public FirstCommand(Content content){
            this.context = content;
        }
    
        /**
         *  @Author: caesar
         *  @Date:2020年11月27日 10:11:27
         *  @Description: 执行命令
         */
        @Override
        public void execute() {
            System.out.println("命令执行中。。。。。。"+this.context.getName());
        }
    
    }
    package com.example.demo.sjms.minglingmoshi;
    
    /**
     *  @Author: caesar
     *  @Date:2020年11月27日 10:11:58
     *  @Description: 第二命令
     */
    public class SecondCommand extends Command{
        public SecondCommand(Content content){
            this.context = content;
        }
        /**
         *  @Author: caesar
         *  @Date:2020年11月27日 10:11:18
         *  @Description: 执行命令
         */
        @Override
        public void execute() {
            System.out.println("命令执行中。。。。。。"+this.context.getName());
        }
    
    }

    调用命令类:

    package com.example.demo.sjms.minglingmoshi;
    
    import lombok.Data;
    
    /**
     *  @Author: caesar
     *  @Date:2020年11月27日 10:11:08
     *  @Description: 命令调用者
     */
    @Data
    public class Invoker {
        private Command command;
        public Invoker(Command command){
            this.command = command;
        }
    
        /**
         *  @Author: caesar
         *  @Date:2020年11月27日 10:11:40
         *  @Description: 服务调用
         */
        public void execute() {
            command.execute();
        }
    
    }

    测试类:

    package com.example.demo.sjms.minglingmoshi;
    
    /**
     *  @Author: caesar
     *  @Date:2020年11月27日 10:11:59
     *  @Description: 测试类
     */
    public class Test {
        public static void main(String[] args) {
            // 实体类
            Content contentA = new Content();
            contentA.setName("contentA");
            Content contentB = new Content();
            contentB.setName("contentB");
            // 命令类
            FirstCommand firstCommand = new FirstCommand(contentA);
            SecondCommand secondCommand  = new SecondCommand(contentB);
            // 调用者
            Invoker invoker = new Invoker(firstCommand);
            invoker.execute();
            invoker.setCommand(secondCommand);
            invoker.execute();
            Invoker invoker1 = new Invoker(secondCommand);
            invoker1.execute();
            invoker1.setCommand(firstCommand);
            invoker1.execute();
    
        }
    }

    四、源码级别

    五、总结

    主要解决:在软件系统中,行为请求者与行为实现者通常是一种紧耦合的关系,但某些场合,比如需要对行为进行记录、撤销或重做、事务等处理时,这种无法抵御变化的紧耦合的设计就不太合适。

  • 相关阅读:
    PAT B1045 快速排序 (25 分)
    PAT B1042 字符统计 (20 分)
    PAT B1040 有几个PAT (25 分)
    PAT B1035 插入与归并 (25 分)
    PAT B1034 有理数四则运算 (20 分)
    PAT B1033 旧键盘打字 (20 分)
    HDU 1231 最大连续子序列
    HDU 1166 敌兵布阵
    HDU 1715 大菲波数
    HDU 1016 Prime Ring Problem
  • 原文地址:https://www.cnblogs.com/mcjhcnblogs/p/14046847.html
Copyright © 2011-2022 走看看