Real world example of application of the command pattern

后端 未结 4 2039
我在风中等你
我在风中等你 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:04

    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.

提交回复
热议问题