computer science 313 advanced programming topics n.
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 20: Implementing Commands PowerPoint Presentation
Download Presentation
Lecture 20: Implementing Commands

Loading in 2 Seconds...

play fullscreen
1 / 21

Lecture 20: Implementing Commands - PowerPoint PPT Presentation

  • Uploaded on

Computer Science 313 – Advanced Programming Topics. Lecture 20: Implementing Commands. Using a Design Pattern. Design patterns are tools Each pattern has reason for being Choose matching pattern to design need Design & coding is simple if choosing correctly

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Lecture 20: Implementing Commands' - druce

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
using a design pattern
Using a Design Pattern
  • Design patterns are tools
    • Each pattern has reason for being
  • Choose matching pattern to design need
    • Design & coding is simple if choosing correctly
    • Life sucks when forced; coding is impossible
  • Knowing pattern intent crucial for good design
    • Make use as simple as adding pattern to client code
command pattern intent
Command Pattern Intent
  • What is the intent of command pattern?
decoupling without texts
Decoupling Without Texts
  • Command pattern decouples (splits)

What we want to do

Who does it

When it is done

decoupling without texts1
Decoupling Without Texts
  • Command pattern decouples (splits)

What we want to do

Who does it

When it is done

command of command pattern
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 interchangablefor design purposes
warning warning warning1
Warning! Warning! Warning!
  • Caution is needed if Command is abstract class
    • All ConcreteCommandsuse 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 Contextclass executing the strategy
    • Method in strategy passed field as parameter
command interface example
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();


invoker of command pattern
Invoker of Command Pattern
  • Stores Commandinstance(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
invoker class example
Invoker Class Example
  • Blindly calls executefor correct Command
    • Select one if multiple Commandsreferred to
    • Simple class that does not care what happens

public class Remote {Command[] btnCommands; void buttonWasPushed(int slot){btnCommands[slot].execute();}


receiver in command pattern
Receiver in Command Pattern
    • With ConcreteCommand, performs the action
    • Real work done by Receiver
    • Uses Commandtomake & order calls
  • Independent of Invoker
    • Lacks knowledge of how it is called
  • Also called functor or callback
receiver class example
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; }


concretecommand in pattern
ConcreteCommandin 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
concretecommand example
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(); }}

multicommand example
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();}

complexcommand example
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);;}

macrocommand example
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();}


client in command pattern
Client in Command Pattern
  • Helps perform actual action
    • Works with ConcreteCommand
    • Sets the Invoker’sCommands
  • Not involved in execution of actions
client class example
Client Class Example
  • Creates Commandsfor program to use
    • Gets references to the Receivers
    • Sets Commandfor 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);

for next class
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?
  • Midterm #1 in class on Friday
    • Open-book, open-note exam (as usual)
    • Have been given & should use pattern reports
    • No computers, slides, or friends, however