程序代写代做代考 Supporting Undo/Redo in the Spreadsheet Application

Supporting Undo/Redo in the Spreadsheet Application
Cpt S 321 Washington State University

HW8
• Two main tasks:
• Add support for background color for cells
• Support undo/redo for text and background color changes of cells

Encapsulating a method invocation?!?

Command Design Pattern
• The Command Design Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.
• This kind of operation will be key in building “undo” and “redo” options in an application
• Also known as: Action, Transaction





○ ○
How does it work?
An object is used to represent and encapsulate all the information needed to call a method at a later time
This information includes the method name, the object that owns the method, and the values for the method parameters
Three fundamental Command pattern actors:
Client: Instantiates command object and provides information to call the method at a later time
Invoker: Executes the Command possibly at a later time
Receiver: The instance of the class that contains the method’s code (i.e., the object the command should affect)

The main actors

(or unexecute())

Coding exercise! (Open laptop and start VS!)
• Implement a remote control using the Command pattern:
• RemoteLoader: client
• RemoteControl: invoker
• Light, CeilingFan, etc.: receivers
• Each slot is assigned a command (define a controller with 7 slots as shown here)
• Let’s start by implementing only one type of receiver: Light

Remember
The Client creates a ConcreteCommand object and specifies its Receiver
● An Invoker object stores the commands
● The Invoker issues a request by calling execute( ) on the command
The ConcreteCommand object invokes operations on its Receiver to carry out the request

Lights…

Even further: undo!
• We want to add functionality to support the undo button on the remote.
• Suppose that the Living Room Light is off and you press the on button on the remote. Obviously the light turns on. Now if you press the undo button then the last action will be reversed in this case, the light will turn off.

Command pattern and undo
• When commands support undo, they have an undo() (or unexecute()) method that mirrors the execute() method. Whatever execute() last did, undo() reverses. So, before we can add undo to our commands, we need to add an undo() method to the Command interface.
• Then, implement the undo() (or unexecute()) method in the LightOnCommand and LightOffCommand. The method has to do the exact opposite of the execute() method.
• Add a field undoCommand in the RemoteControl. Don’t forget to set it every time a button is pushed
• Add a method undoButtonPushed() in the RemoteControl

Command pattern summary
Two important aspects of the Command pattern:
interface separation (the invoker is isolated from the receiver)
time separation (stores a ready-to-go processing request that’s



● ●
● ●
to be stated later)
Easily change Commands without changing existing classes Commands are first-class objects. They can be manipulated and extended like any other object
Allows for a history of commands to be kept (undo & redo)
Can assemble multiple Commands into composite commands, like Macros/ Transactions

References
• Freeman, Eric,Robson, Elisabeth,Bates, Bert,Sierra, Kathy. Head First Design Patterns: A Brain-Friendly Guide (Kindle Location 3107). O’Reilly Media.