java 命令模式的实际应用示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12153708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Real world example of application of the command pattern
提问by Jim
Command patterncan 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
?
命令模式可用于实现Transactional behavior
(和Undo
)。
但是我无法通过谷歌搜索找到这些示例。我只能找到一些简单的例子,说明是switched on
或的灯off
。
我在哪里可以找到Java
使用Command Pattern
?
回答by Vikdor
In one of our projects, we have the following requirement:
在我们的一个项目中,我们有以下要求:
- Create a record in DB.
- Call a service to update a related record.
- Call another service to log a ticket.
- 在 DB 中创建记录。
- 调用服务以更新相关记录。
- 调用另一个服务来记录票证。
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().
为了以事务性方式执行此操作,每个操作都被实现为带有撤消操作的命令。在每一步结束时,命令都会被推送到堆栈中。如果操作在某个步骤失败,那么我们从堆栈中弹出命令并对弹出的每个命令调用撤消操作。每个步骤的撤消操作都在该命令实现中定义,以反转先前的 command.execute()。
Hope this helps.
希望这可以帮助。
回答by oldrinb
public final class Ping implements Callable<Boolean> {
private final InetAddress peer;
public Ping(final InetAddress peer) {
this.peer = peer;
}
public Boolean call() {
/* do the ping */
...
}
}
...
final Future<Boolean> result
= executorService.submit(new Ping(InetAddress.getByName("google.com")));
System.out.println("google.com is " + (result.get() ? "UP" : "DOWN"));
回答by Ekta
Command Patterns are used in a lot of places.
命令模式用在很多地方。
- Of course what you see everywhere is a very trivial example of GUI Implementation, switches. It is also used extensively is game development. With this pattern the user can configure his buttons on screen as well.
- It is used in Networking as well, if a command has to be passed to the other end.
- When the programmers want to store all the commands executed by the user, e.g. sometimes a game lets you replay the whole level.
- It is used to implement callbacks.
- 当然,你在任何地方看到的都是一个非常简单的 GUI 实现、开关示例。它也被广泛用于游戏开发。通过这种模式,用户也可以在屏幕上配置他的按钮。
- 如果必须将命令传递到另一端,它也可用于网络。
- 当程序员想要存储用户执行的所有命令时,例如有时游戏让您重播整个关卡。
- 它用于实现回调。
Here is a site which provides as example of command pattern used for callback. http://www.javaworld.com/article/2077569/core-java/java-tip-68--learn-how-to-implement-the-command-pattern-in-java.html?page=2
这是一个提供用于回调的命令模式示例的站点。http://www.javaworld.com/article/2077569/core-java/java-tip-68--learn-how-to-implement-the-command-pattern-in-java.html?page=2
- Here's another link which shows command pattern with database. The code is in C#. http://www.codeproject.com/Articles/154606/Command-Pattern-at-Work-in-a-Database-Application
- 这是另一个显示数据库命令模式的链接。代码在 C# 中。http://www.codeproject.com/Articles/154606/Command-Pattern-at-Work-in-a-Database-Application
回答by Ravindra babu
You have to define undo(), redo() operations along with execute() in Command interface itself
.
You have to define undo(), redo() operations along with execute() in Command interface itself
.
example:
例子:
interface ChangeI {
enum State{ READY, DONE, UNDONE, STUCK } ;
State getState() ;
void execute() ;
void undo() ;
void redo() ;
}
Define a State in your ConcreteCommand
class. Depending on current State after execute
() method, you have to decide whether command should be added to Undo Stack
or Redo Stack
and take decision accordingly.
在您的ConcreteCommand
班级中定义一个状态。根据当前状态 after execute
() 方法,您必须决定是否应将命令添加到Undo Stack
或Redo Stack
并相应地做出决定。
abstract class AbstractChange implements ChangeI {
State state = State.READY ;
public State getState() { return state ; }
public void execute() {
assert state == State.READY ;
try { doHook() ; state = State.DONE ; }
catch( Failure e ) { state = State.STUCK ; }
catch( Throwable e ) { assert false ; }
}
public void undo() {
assert state == State.DONE ; }
try { undoHook() ; state = State.UNDONE ; }
catch( Failure e ) { state = State.STUCK ; }
catch( Throwable e ) { assert false ; }
}
public void redo() {
assert state == State.UNDONE ;
try { redoHook() ; state = State.DONE ; }
catch( Failure e ) { state = State.STUCK ; }
catch( Throwable e ) { assert false ; }
}
protected abstract void doHook() throws Failure ;
protected abstract void undoHook() throws Failure ;
protected void redoHook() throws Failure { doHook() ;} ;
}
Have a look at this undo-redocommand article for better understanding.
看看这个undo-redo命令文章以获得更好的理解。