Real world example of application of the command pattern

后端 未结 4 2045
我在风中等你
我在风中等你 2021-02-08 01:46

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

4条回答
  •  心在旅途
    2021-02-08 02:25

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

    1. Create a record in DB.
    2. Call a service to update a related record.
    3. Call another service to log a ticket.

    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.

提交回复
热议问题