Week 6/7 Flashcards
Various Degrees of Undo:
- Undo last (one level)2. Limited undo (fixed nr of levels)3. Arbitrary undo (limited by memory only)
Various Degree of Redo:
- Not available2. Redo last3. Limited redo4. Arbitrary undo
Solutions for Undo Redo Facility
- Storing full model states:- one stack of states for undo and one for redo- requires the ability to clone and restore a given state- pro is that it is simple, but there is a performance loss when the state is large
- Storing state-changing commands:- user invokes commands via GUI, changing data in the models- these commands are stored in two stacks, one of executed commands for undo, and one of undone-but-redoable commands for redo- commands are stored using Command design pattern- commands must have the ability to revert functionality
Command design pattern
encapsulates a request as an object, allowing for parameterization of clients with different requests, queuing of requests, and the ability to support undoable operationsaims to decouple the sender of a request from the object that performs the request, promoting flexibility, extensibility, and better organisation of code
Components of Command Design Pattern
- Command:- interface/abstract class that declares the execute method2. Concrete Command:- implements Command interface and represents a specific command with a set of actions- holds a reference to the object on which the actions should be perceived (the receiver)3. Invoker:- class responsible for invoking commands- holds a reference to a command object and calls its execute method4. Receiver:- class that performs the actual operation
Code Example for Command Design Pattern
// Command interfaceinterface Command { void execute();}// Concrete command classesclass LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { this.light = light; } @Override public void execute() { light.turnOn(); }}class LightOffCommand implements Command { private Light light; public LightOffCommand(Light light) { this.light = light; } @Override public void execute() { light.turnOff(); }}// Receiver classclass Light { void turnOn() { System.out.println(“Light is ON”); } void turnOff() { System.out.println(“Light is OFF”); }}// Invoker classclass RemoteControl { private Command command; void setCommand(Command command) { this.command = command; } void pressButton() { command.execute(); }}
Client Code Example for Command Design Pattern
// Client codepublic class CommandPatternExample { public static void main(String[] args) { // Creating instances of the receiver and concrete command classes Light light = new Light(); Command lightOn = new LightOnCommand(light); Command lightOff = new LightOffCommand(light); // Creating an invoker and associating commands RemoteControl remote = new RemoteControl(); remote.setCommand(lightOn); // Pressing the button, which will execute the associated command remote.pressButton(); // Changing the command associated with the invoker remote.setCommand(lightOff); remote.pressButton(); }}
Relation of Command Design Pattern to Strategy Design Pattern
command interface can be viewed as a strategy interface with concrete commands as concrete strategiesinvoker plays role of context in strategy pattern and only knows about abstract commandclient plays client role of strategy pattern and selects/creates concrete commands
Undo via Command Design Pattern
Invovles providing each concrete command with a revert method to revert the effect of execute.Moreover, a command undo stack must be introduced.The controller creatses Command objects, executes them, and then pushes them onto the undo stack.Undo is implemented by popping a command from the undo stack and invoking its revert method.
Compound Command Design Pattern
allows clients to treat a group of commands as a single commandparticularly useful for implementing undo/redo functionality or for managing complex sequences of operations
Components of Compound Command Design Pattern
- Command Interface:- an interface/ abstract class with an execute method2. Concrete Command:- implements the Command interface, representing individual commands- holds a reference to a receiver, on which the action is performed3. Receiver:- represents the object that performs the actual actions4. Compound/Macro Command:- implements the Command interface, but internally manages a collection of other commands- its execute method executes the execute methods of all the commands it holds references too5. Invoker:- invokes the actual compound command
Code Example for Compound Command Pattern
// Command interfaceinterface Command { void execute();}// Concrete command classesclass LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { this.light = light; } @Override public void execute() { light.turnOn(); }}class LightOffCommand implements Command { private Light light; public LightOffCommand(Light light) { this.light = light; } @Override public void execute() { light.turnOff(); }}// Compound command classclass MacroCommand implements Command { private List commands; public MacroCommand() { commands = new ArrayList<>(); } public void addCommand(Command command) { commands.add(command); } @Override public void execute() { for (Command command : commands) { command.execute(); } }}// Receiver classclass Light { void turnOn() { System.out.println(“Light is ON”); } void turnOff() { System.out.println(“Light is OFF”); }}// Invoker classclass RemoteControl { private Command command; void setCommand(Command command) { this.command = command; } void pressButton() { command.execute(); }}
Client Code for Compound Command pattern
public class CompoundCommandExample { public static void main(String[] args) { // Creating instances of the receiver and concrete command classes Light light = new Light(); Command lightOn = new LightOnCommand(light); Command lightOff = new LightOffCommand(light); // Creating a compound command and adding individual commands to it MacroCommand macroCommand = new MacroCommand(); macroCommand.addCommand(lightOn); macroCommand.addCommand(lightOff); // Creating an invoker and associating the compound command RemoteControl remote = new RemoteControl(); remote.setCommand(macroCommand); // Pressing the button, which will execute the associated compound command remote.pressButton(); }}
Backtracking
an algorithmic approach that incrementally builds and explores potential solutions to a problem, abandoning and backtracking when it determines that a partial solution cannot be completed to a valid solutionoften used for solving constraint satisfaction problems by systematically exploring the search space of possibilities
Backtracking Algorithm for Puzzle Assistant
- Apply reasoning to find “forced” digits.2. Find an open cell c. If not found and the puzzle is valid, then puzzle is solved. If not found and the puzzle is not valid, then backtrack (if possible).3. If open cell c found, for each possible cell state s, speculatively, set c to s, and if state still valid, solve remainder of puzzle recursively.