实际应用命令模式的例子 [英] Real world example of application of the command pattern

查看:174
本文介绍了实际应用命令模式的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令模式可用于实现事务行为(和撤消)。

但是我没有通过谷歌搜索找到这些的例子。我只能找到开启关闭的灯泡的一些简单的例子。

在哪里可以找到使用命令模式实现的这些行为的编码示例(最好在 Java ) / p>

Command pattern can be used to implement Transactional behavior (and Undo).
But I could not find an example of these by googling. I could only find some trivial examples of a lamp that is switched on or off.
Where can I find a coding example (preferably in Java)of this/these behaviors implemented using the Command Pattern?

推荐答案

在我们的一个项目中,我们有以下要求:

In one of our projects, we have the following requirement:


  1. 在DB中创建记录

  2. 致电服务以更新相关记录。

  3. 调用其他服务记录

要以事务方式执行此操作,每个操作都将实现为具有撤消操作的命令。在每个步骤结束时,命令被推到堆栈上。如果操作在某个步骤失败,那么我们从栈中弹出命令,并在弹出的每个命令上调用undo操作。每个步骤的撤消操作在该命令实现中定义,以反转较早的command.execute()。

To perform this in a transactional manner, each operation is implemented as a command with undo operation. At the end of each step, the command is pushed onto a stack. If the operation fails at some step, then we pop the commands from the stack and call undo operation on each of the command popped out. The undo operation of each step is defined in that command implementation to reverse the earlier command.execute().

希望这有帮助。

这篇关于实际应用命令模式的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆