lecture 24: implementing commands
DESCRIPTION
Computer Science 313 – Advanced Programming Topics. Lecture 24: Implementing Commands. Command Pattern Decoupling. What we want to do Who does it When it is done. Command Pattern Decoupling. What we want to do Who does it When it is done. Command of Command Pattern. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/1.jpg)
LECTURE 24:IMPLEMENTING COMMANDS
Computer Science 313 – Advanced Programming Topics
![Page 2: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/2.jpg)
Command Pattern Decoupling
What we want to doWho does itWhen it is done
![Page 3: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/3.jpg)
Command Pattern Decoupling
What we want to doWho does itWhen it is done
![Page 4: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/4.jpg)
Command of Command Pattern Command declares method(s) defining
actions Limit to fewest methods undo included, if desired
Must be abstract type Interface is possible Can be abstract class Terms interchangable
for design purposes
![Page 5: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/5.jpg)
Warning! Warning! Warning!
![Page 6: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/6.jpg)
Warning! Warning! Warning! Caution is needed if Command is
abstract class All ConcreteCommands use fields being
declared Big hint suggesting Command Pattern
incorrect Abstracts what is executed from the code Should be casting as wide a net as
possible Requiring common details is a rather
limited net Consider using Strategy Pattern may
be better Add fields to Context class executing the
strategy Method in strategy passed field as
parameter
![Page 7: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/7.jpg)
Command Interface Example
Should be short & simple Use additional methods if more are
needed Often methods void for maximum
usability Returns value to Receiver in other cases
public interface Command {public void execute();public void undo();
}
![Page 8: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/8.jpg)
Invoker of Command Pattern Stores Command instance(s)
Only refers to interface May change object held Changes made externally
Starts action execution Any reason valid forwhy this happens
Actual execution maybe delayed
![Page 9: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/9.jpg)
Invoker Class Example
Blindly calls execute for correct Command Select one if multiple Commands referred
to Simple class that does not care what
happens
public class Remote { Command[] btnCommands; void buttonWasPushed(int slot){ btnCommands[slot].execute(); }
}
![Page 10: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/10.jpg)
Receiver in Command Pattern With ConcreteCommand, performs the
action Real work done by Receiver Uses Command to
make & order calls Independent of Invoker Lacks knowledge
of how it is called Also called
FUNCTOR or CALLBACK
![Page 11: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/11.jpg)
Receiver Class Example
Has methods needed to execute action Can be written to split work across method
public class CDPlayer { boolean powered, trayOpen; public void turnOn() { powered = true; } public void closeTray() { trayOpen = false; }}
![Page 12: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/12.jpg)
ConcreteCommand in Pattern Calls method(s) in Receiver to perform
action Includes fields it will need Also acts on own
Separates Invoker &Receiver Prevents from
needing the other State defines it &
its actions
![Page 13: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/13.jpg)
ConcreteCommand Example
class LtCommand implements Command {Light bulb;public LtCommand(Light l) { bulb = l;}public void execute() { bulb.turnOn();}public void undo() { if (bulb.on()) { bulb.turnOff(); } else { bulb.turnOn(); }}
![Page 14: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/14.jpg)
MultiCommand Example
Party mode combines multiple Receivers
class PartyCommand implements Command { Stereo stereo; public MultiCommand(Stereo s) { stereo = s; } public void execute() { stereo.turnOn(); stereo.closeTray(); }
![Page 15: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/15.jpg)
ComplexCommand Example
Can also perform actions on its own
class OpenCommand implements Command { Application app; public void execute() { String name = askUser();
doc = new Document(); app.add(doc); doc.open();
}
![Page 16: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/16.jpg)
MacroCommand Example
Define macros which compose Commands
class MacroCommand implements Command {ArrayList<Command> cmd;public void addCommand(Command c) { cmd.add(c);}public void execute() {
for (Command c : cmd) c.execute();}
}
![Page 17: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/17.jpg)
Client in Command Pattern
Helps perform actual action Works with ConcreteCommand Sets the Invoker’sCommands
Not involved in execution of actions
![Page 18: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/18.jpg)
Client Class Example
Creates Commands for program to use Gets references to the Receivers Sets Command for the Invokers
Command lc = new LtCommand(light);Command sc = new StereoCommand(s); Command mc = new MacroCommand();mc.addCommand(lc);mc.addCommand(sc);remote.setOnCommand(0, lc);remote.setOnCommand(1, sc); remote.setOnCommand(2, mc);
![Page 19: Lecture 24: Implementing Commands](https://reader035.vdocument.in/reader035/viewer/2022062501/568162b1550346895dd33845/html5/thumbnails/19.jpg)
For Next Class
Read remainder of the chapter Complete command pattern lecture &
discussion How would we go about creating logs of Commands?
Why does CSC351 need this pattern & how is it good?
And what in the world could CSC310 need this work?
Lab #5 on Angel & can rework on Friday Hopefully, should reuse all your existing
code Retrofit to use pattern by factoring existing
code