设计模式系列—命令模式

开发 前端
本篇和大家一起来学习命令模式相关内容。

模式定义

将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行储存、传递、调用、增加与管理。

在软件开发系统中,常常出现“方法的请求者”与“方法的实现者”之间存在紧密的耦合关系。这不利于软件功能的扩展与维护。例如,想对行为进行“撤销、重做、记录”等处理都很不方便,因此“如何将方法的请求者与方法的实现者解耦?”变得很重要,命令模式能很好地解决这个问题。

模版实现如下:

package com.niuh.designpattern.command.v1; 
 
/** 
 * <p> 
 * 命令模式 
 * </p> 
 */ 
public class CommandPattern { 
    public static void main(String[] args) { 
        Command cmd = new ConcreteCommand(); 
        Invoker ir = new Invoker(cmd); 
        System.out.println("客户访问调用者的call()方法..."); 
        ir.call(); 
    } 

 
//抽象命令 
interface Command { 
    public abstract void execute(); 

 
//具体命令 
class ConcreteCommand implements Command { 
    private Receiver receiver; 
 
    ConcreteCommand() { 
        receiver = new Receiver(); 
    } 
 
    public void execute() { 
        receiver.action(); 
    } 

 
//接收者 
class Receiver { 
    public void action() { 
        System.out.println("接收者的action()方法被调用..."); 
    } 

 
//调用者 
class Invoker { 
    private Command command; 
 
    public Invoker(Command command) { 
        this.command = command; 
    } 
 
    public void setCommand(Command command) { 
        this.command = command; 
    } 
 
    public void call() { 
        System.out.println("调用者执行命令command..."); 
        command.execute(); 
    } 

  • 1.
  • 2.
  • 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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.

输出结果如下:

客户访问调用者的call()方法... 
调用者执行命令command... 
接收者的action()方法被调用... 
  • 1.
  • 2.
  • 3.

解决的问题

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

模式组成

 

可以将系统中的相关操作抽象成命令,使调用者与实现者相关分离,其结构如下。

实例说明

实例概况

 

结合命令模式,实现一个课程视频的打开和关闭。

使用步骤

 

步骤1:声明执行命令的接口,拥有执行命令的抽象方法 execute()

interface Command { 
    void execute(); 

  • 1.
  • 2.
  • 3.

步骤2:定义具体命令角色,创建打开课程链接 和 关闭课程连接

/** 
 * 打开课程链接 
 */ 
class OpenCourseVideoCommand implements Command { 
 
    private CourseVideo courseVideo; 
 
    public OpenCourseVideoCommand(CourseVideo courseVideo) { 
        this.courseVideo = courseVideo; 
    } 
 
    @Override 
    public void execute() { 
        courseVideo.open(); 
    } 

 
/** 
 * 关闭课程链接 
 */ 
class CloseCourseVideoCommand implements Command { 
 
    private CourseVideo courseVideo; 
 
    public CloseCourseVideoCommand(CourseVideo courseVideo) { 
        this.courseVideo = courseVideo; 
    } 
 
    @Override 
    public void execute() { 
        courseVideo.close(); 
    } 

  • 1.
  • 2.
  • 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.

步骤3:定义接收者角色,执行命令功能的相关操作,是具体命令对象业务的真正实现者

class CourseVideo { 
 
    private String name
 
    public CourseVideo(String name) { 
        this.name = name
    } 
 
    public void open() { 
        System.out.println(this.name + "课程视频开放。"); 
    } 
 
    public void close() { 
        System.out.println(this.name + "课程视频关闭。"); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

步骤4:创建User对象为请求的发送者,即请求者角色

class User { 
 
    private List<Command> commands = new ArrayList<>(); 
 
    public void addCommand(Command command) { 
        commands.add(command); 
    } 
 
    public void executeCommands() { 
        commands.forEach(Command::execute); 
        commands.clear(); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

步骤5:测试执行

public class CommandPattern { 
 
    public static void main(String[] args) { 
        //命令接收者 
        CourseVideo courseVideo = new CourseVideo("设计模式系列"); 
 
        //创建命令 
        OpenCourseVideoCommand openCourseVideoCommand = new OpenCourseVideoCommand(courseVideo); 
        CloseCourseVideoCommand closeCourseVideoCommand = new CloseCourseVideoCommand(courseVideo); 
 
        //创建执行人 
        User user = new User(); 
 
        //添加命令 
        user.addCommand(openCourseVideoCommand); 
        user.addCommand(closeCourseVideoCommand); 
 
        //执行 
        user.executeCommands(); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

输出结果

  • 设计模式系列课程视频开放。
  • 设计模式系列课程视频关闭。

优点

  1. 降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。
  2. 增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。
  3. 可以实现宏命令。命令模式可以与组合模式结合,将多个命令装配成一个组合命令,即宏命令。
  4. 方便实现 Undo 和 Redo 操作。命令模式可以与后面介绍的备忘录模式结合,实现命令的撤销与恢复。

缺点

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

应用场景

命令执行过程较为复杂且可能存在变化,需要对执行命令动作本身进行额外操作,此时可以考虑使用命令模式

命令模式的扩展

 

在软件开发中,有时将命令模式与组合模式联合使用,这就构成了宏命令模式,也叫组合命令模式。宏命令包含了一组命令,它充当了具体命令与调用者的双重角色,执行它时将递归调用它所包含的所有命令,其具体结构图如下:

模版实现如下:

package com.niuh.designpattern.command.v2; 
 
import java.util.ArrayList; 
 
/** 
 * <p> 
 * 组合命令模式 
 * </p> 
 */ 
public class CompositeCommandPattern { 
    public static void main(String[] args) { 
        AbstractCommand cmd1 = new ConcreteCommand1(); 
        AbstractCommand cmd2 = new ConcreteCommand2(); 
        CompositeInvoker ir = new CompositeInvoker(); 
        ir.add(cmd1); 
        ir.add(cmd2); 
        System.out.println("客户访问调用者的execute()方法..."); 
        ir.execute(); 
    } 

 
//抽象命令 
interface AbstractCommand { 
    public abstract void execute(); 

 
//树叶构件: 具体命令1 
class ConcreteCommand1 implements AbstractCommand { 
    private CompositeReceiver receiver; 
 
    ConcreteCommand1() { 
        receiver = new CompositeReceiver(); 
    } 
 
    public void execute() { 
        receiver.action1(); 
    } 

 
//树叶构件: 具体命令2 
class ConcreteCommand2 implements AbstractCommand { 
    private CompositeReceiver receiver; 
 
    ConcreteCommand2() { 
        receiver = new CompositeReceiver(); 
    } 
 
    public void execute() { 
        receiver.action2(); 
    } 

 
//树枝构件: 调用者 
class CompositeInvoker implements AbstractCommand { 
    private ArrayList<AbstractCommand> children = new ArrayList<AbstractCommand>(); 
 
    public void add(AbstractCommand c) { 
        children.add(c); 
    } 
 
    public void remove(AbstractCommand c) { 
        children.remove(c); 
    } 
 
    public AbstractCommand getChild(int i) { 
        return children.get(i); 
    } 
 
    public void execute() { 
        for (Object obj : children) { 
            ((AbstractCommand) obj).execute(); 
        } 
    } 

 
//接收者 
class CompositeReceiver { 
    public void action1() { 
        System.out.println("接收者的action1()方法被调用..."); 
    } 
 
    public void action2() { 
        System.out.println("接收者的action2()方法被调用..."); 
    } 

  • 1.
  • 2.
  • 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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.

输出结果如下:

  • 客户访问调用者的execute()方法...
  • 接收者的action1()方法被调用...
  • 接收者的action2()方法被调用...

命令模式还可以同备忘录(Memento)模式组合使用,这样就变成了可撤销的命令模式

源码中的应用

  • java.util.Timer类中scheduleXXX()方法
  • java Concurrency Executor execute() 方法
  • java.lang.reflect.Method invoke()方法
  • org.springframework.jdbc.core.JdbcTemplate
  • ......

在 JdbcTemplate 中的应用

在JdbcTemplate中命令模式的使用并没有遵从标准的命令模式的使用,只是思想相同而已。

 

在 Spring 的 JdbcTemplate 这个类中有 query() 方法,query() 方法中定义了一个内部类 QueryStatementCallback,QueryStatementCallback 又实现了 StatementCallback 接口,另外还有其它类实现了该接口,StatementCallback 接口中又有一个抽象方法 doInStatement()。在 execute() 中又调用了 query()。

StatementCallback充当的是命令角色,JdbcTemplate即充当调用者角色,又充当接收者角色。上面的类图只是为了方便理解,实际上,QueryStatementCallback 与 ExecuteStatementCallback是JdbcTemplate中方法的内部类,具体看源码中的内容。

部分源码分析

 

StatementCallback接口:

public interface StatementCallback<T> { 
 T doInStatement(Statement stmt) throws SQLException, DataAccessException; 

  • 1.
  • 2.
  • 3.

JdbcTemplate类:

public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { 
 //相当于调用者发布的一个命令 
 @Override 
 public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException { 
  return query(sql, new RowMapperResultSetExtractor<T>(rowMapper)); 
 } 
 //命令发布后由具体的命令派给接收者进行执行 
 @Override 
 public <T> T query(final String sql, final ResultSetExtractor<T> rse) throws DataAccessException { 
  Assert.notNull(sql, "SQL must not be null"); 
  Assert.notNull(rse, "ResultSetExtractor must not be null"); 
  if (logger.isDebugEnabled()) { 
   logger.debug("Executing SQL query [" + sql + "]"); 
  } 
  //内部类,实现StatementCallback,相当于具体的命令 
  class QueryStatementCallback implements StatementCallback<T>, SqlProvider { 
   @Override 
   public T doInStatement(Statement stmt) throws SQLException { 
    ResultSet rs = null
    try { 
     rs = stmt.executeQuery(sql); 
     ResultSet rsToUse = rs; 
     if (nativeJdbcExtractor != null) { 
      rsToUse = nativeJdbcExtractor.getNativeResultSet(rs); 
     } 
     return rse.extractData(rsToUse); 
    } 
    finally { 
     JdbcUtils.closeResultSet(rs); 
    } 
   } 
   @Override 
   public String getSql() { 
    return sql; 
   } 
  } 
  return execute(new QueryStatementCallback()); 
 } 
 //相当于接收者,命令真正的执行者 
 @Override 
 public <T> T execute(StatementCallback<T> action) throws DataAccessException { 
  Assert.notNull(action"Callback object must not be null"); 
  Connection con = DataSourceUtils.getConnection(getDataSource()); 
  Statement stmt = null
  try { 
   Connection conToUse = con; 
   if (this.nativeJdbcExtractor != null && 
     this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) { 
    conToUse = this.nativeJdbcExtractor.getNativeConnection(con); 
   } 
   stmt = conToUse.createStatement(); 
   applyStatementSettings(stmt); 
   Statement stmtToUse = stmt; 
   if (this.nativeJdbcExtractor != null) { 
    stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt); 
   } 
   T result = action.doInStatement(stmtToUse); 
   handleWarnings(stmt); 
   return result; 
  } 
  catch (SQLException ex) { 
   // Release Connection early, to avoid potential connection pool deadlock 
   // in the case when the exception translator hasn't been initialized yet. 
   JdbcUtils.closeStatement(stmt); 
   stmt = null
   DataSourceUtils.releaseConnection(con, getDataSource()); 
   con = null
   throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex); 
  } 
  finally { 
   JdbcUtils.closeStatement(stmt); 
   DataSourceUtils.releaseConnection(con, getDataSource()); 
  } 
 } 

  • 1.
  • 2.
  • 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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.

PS:以上代码提交在 Github :

 

https://github.com/Niuh-Study/niuh-designpatterns.git

责任编辑:姜华 来源: 今日头条
相关推荐

2022-01-12 13:33:25

工厂模式设计

2020-10-23 09:40:26

设计模式

2020-11-04 08:54:54

状态模式

2021-06-09 08:53:34

设计模式策略模式工厂模式

2021-03-02 08:50:31

设计单例模式

2020-10-21 14:29:15

原型模式

2022-01-14 09:22:22

设计模式桥接

2020-10-19 09:28:00

抽象工厂模式

2013-11-26 15:48:53

Android设计模式SDK

2021-09-29 13:53:17

抽象工厂模式

2023-05-04 08:47:31

命令模式抽象接口

2021-10-26 00:21:19

设计模式建造者

2020-10-28 11:56:47

桥接模式

2021-10-28 19:09:09

模式原型Java

2020-11-09 08:20:33

解释器模式

2012-01-13 15:59:07

2020-10-20 13:33:00

建造者模式

2020-11-05 09:38:07

中介者模式

2021-03-05 07:57:41

设计模式桥接

2020-10-26 08:45:39

观察者模式
点赞
收藏

51CTO技术栈公众号