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
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- how to define constructor for Python's new Nam
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
You have to define undo(), redo() operations along with execute() in Command interface itself
.example:
Define a State in your
ConcreteCommand
class. Depending on current State afterexecute
() method, you have to decide whether command should be added toUndo Stack
orRedo Stack
and take decision accordingly.Have a look at this undo-redo command article for better understanding.
Command Patterns are used in a lot of places.
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
In one of our projects, we have the following requirement:
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().
Hope this helps.