Real world example of application of the command pattern

我的未来我决定 提交于 2019-12-03 11:43:43

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.

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"));

Command Patterns are used in a lot of places.

  1. 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.
  2. It is used in Networking as well, if a command has to be passed to the other end.
  3. When the programmers want to store all the commands executed by the user, e.g. sometimes a game lets you replay the whole level.
  4. It is used to implement callbacks.

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

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

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.

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-redo command article for better understanding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!