130 likes | 422 Views
Using the Command Pattern to Simplify Undo and Redo . Move interface. Abstract class with 2 abstract methods: Execute: apply the move Undo: undo the move Implementation for each type of move Encapsulate the logic of applying a move in the entities. Abstract MoveController.
E N D
Move interface Abstract class with 2 abstract methods: Execute: apply the move Undo: undo the move Implementation for each type of move Encapsulate the logic of applying a move in the entities
AbstractMoveController Also abstract class with one abstract method: execute() Each one is responsible for converting GUI locations into entity objects Each one creates the appropriate type Move
TypeOfMovementController TypeOfMovementController determines the type of movement done on the GUI It instantiates an instance of the MoveController The MoveControllerexecute the move on the entities and stores the Move object in the MoveHistory
MoveHistory, UndoController, and RedoController UndoController and RedoController rely on MoveHisory to implement undo and redo in the entities MoveHistory uses the undo() and execute() methods of the Moveobjects to do the actual manipulation of entities MoveHistory returns the Move to the UndoControlleror RedoController UndoController and RedoController use the contents of the Move to update the GUI
Benefits • Single location stores the logic of how to apply and undo a move. • MoveHistory does not need to know what the moves that it is storing are or how to apply a move. • MoveController does not need to contain any logic of how to update entity objects.
Weaknesses • Even though the MoveController needs to have all the objects that are used to apply the move, it has to call an external method to do it. • MoveController still has four instances, one for each type move, and cannot fully abstract the type of movement. • Chains of moves have intermediate locations stored twice, once as the end point of one move and again as the starting point of another. • Requires the supporting method TypeOfMovementController to determine the move.