Design Pattern 12-Command

xiaoxiao2021-03-06  60

Using system; use system.collections; namespace pattern {public interface command {void execute (); void undo ();

} // where execute is used to execute commands, and Undo is used to recover (undo).

// Next, this interface is implemented, first implemented: public class cutcommand: command {public void execute () {public void execute () {/ * backup old data * / / * Cut * /

}

Public void undo () {/ * restored to backup data * /}

}

// Re-implement a delete command: public class deleteCommand: command {public void execute () {/ * backup old data * / / * delete * /}

Public void undo () {/ * restored to backup data * /}

}

// If we do another editing action, we will perform a corresponding Command. Next we have to consider saving these executed commands to implement undo / redo. We designed a CommandManager: public interface commandManager {void storeCommand (Command cmd); void clearAllCommand (); void undo (); void redo ();} // again achieve a CommandManager, we call CommandHistoryManager: public class CommandHistoryManager: CommandManager {ArrayList undoList = new ArrayList (); ArrayList redoList = new arraylist (); public void storecommand (Command cmd) {undolist.add (cmd);} public void clearllcommand () {undolist.clear (); redolist.clear ();} public void undo () {= {(undolist) .Count <= 0) Return; Command cmd = (Command) undolist [undolist.count -1]; cmd.undo (); undolist.remove (cmd); redolist.add (cmd);} public void redo () { IF (redolist.count <= 0) return; Command cmd = (command) redolist [redolist.count -1]; cmd.execute (); redolist.remove (cmd); undolist.add (cmd);}}

}

转载请注明原文地址:https://www.9cbs.com/read-111836.html

New Post(0)