Command Design Pattern - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Command Design Pattern

Description:

The first object (the 'sender') could hold a reference to the second (the 'receiver' ... Write a list of commands for later use. String fn = 'logfile' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 18
Provided by: rickm1
Category:

less

Transcript and Presenter's Notes

Title: Command Design Pattern


1
Command Design Pattern
  • Rick Mercer
  • and Rice University

2
Relationship among Objects
  • When two objects communicate, often one object is
    sending a message to a receiver to perform a
    particular function  
  • The first object  (the "sender") could hold a
    reference to the second (the "receiver") 
  • or get it as a return value, or argument, or
    construct it
  • The senders sends a specific method to the
    receiver

3
The Command Design Pattern
  • But what if the sender is not aware of, or does
    not care who the receiver is?  
  • The Command design pattern encapsulates the
    concept of a "Command" as an object
  • The sender holds a reference to a Command object
    rather than to the specific receiver 
  • The Command object encapsulates the receiver

4
  • The sener sends a vanilla message (such as
    actionPerformed, execute, doit, or undo) to the
    Command object
  • The Command object is then responsible for
    dispatching the correct messages to the specific
    receiver(s) to get the job done

5
Command Pattern in Java
  • One object can send messages to other objects
    without knowing anything about the actual
    operation or the type of object
  • Polymorphism lets us encapsulate a request for
    services as an object
  • Establish a method signature name as an interface
  • Vary the algorithms in the called methods

6
Java Examples
  • Sun used the Command pattern to improve the event
    model in Java 1.1
  • one example method signature
  • public void actionPerfomed(ActionEvent e)
  • JButtons and JTextFields send actionPerformed
    messages to "command" objects (the listeneres)
    without knowing what will happen
  • Nor do they care
  • Event generators buttons, text fields, mouse
    have listener objects
  • Different way to implement the same pattern

7
Uses
  • The Command object can also be used when you need
    to tell the program to execute the command later.
  • In such cases, you are saving commands as objects
    to be executed later
  • You could be sending command objects over the
    network or saving them in a collection class
  • Game of Life needs only one 2D array if the
    growth and death of cells are stored as commands

8
General Form of Command
9
Example
  • Make 3 command classes
  • Log instances by writing the objects to a file
  • Read the objects later and execute them
  • // Use these Persistence details
  • String fileName "logfile"
  • FileOutputStream bytesToDisk
  • new FileOutputStream(fileName)
  • ObjectOutputStream objectToBytes
  • new ObjectOutputStream(bytesToDisk)
  • // Store command as an object to exectute later
  • WorkCommand command new DomesticEngineer()
  • objectToBytes.writeObject(command)

10
A UML View of the Sample
11
  • // Command design pattern
  • // Decoupling producer from consumer
  • public interface WorkCommand
  • void execute()
  • class DomesticEngineer implements WorkCommand,
    Serializable
  • public void execute()
  • System.out.println("take out the trash")
  • class Politician implements WorkCommand,
    Serializable
  • public void execute()
  • System.out.print("take money from the rich,
    ")
  • System.out.println("take votes from the
    poor")

12
  • class Programmer implements WorkCommand,
    Serializable
  • public void execute()
  • System.out.print("sell the bugs, ")
  • System.out.println("charge extra for the
    fixes")

13
  • // Write a list of commands for later use
  • String fn "logfile"
  • FileOutputStream bytesToDisk new
    FileOutputStream(fn)
  • ObjectOutputStream objectToBytes
  • new ObjectOutputStream(bytesToDisk)
  • // Imagine commands are some important logged
    transaction
  • WorkCommand commandWork new
    DomesticEngineer()
  • WorkCommand commandPol new Politician()
  • WorkCommand commandProg new Programmer()
  • // Create a list to save for later use
  • ArrayList allCommands new ArrayList()
  • allCommands.add(commandWork)
  • allCommands.add(commandPol)
  • allCommands.add(commandProg) // add more,
    mix 'em up
  • // Save the list to a file for later use

14
  • // Read a list of commands stored as objects
    // and execute them
  • String fileName "logfile"
  • FileInputStream diskToStreamOfBytes null
  • ObjectInputStream bytesToObjects null
  • List list null
  • diskToStreamOfBytes new FileInputStream(file
    Name)
  • bytesToObjects new ObjectInputStream(diskToS
    treamOfBytes)
  • list (ArrayList) bytesToObjects.readObject()
  • bytesToObjects.close()
  • WorkCommand command null
  • for (WorkCommand command list)

15
Summary
  • The Command design pattern encapsulates the
    concept of a command into an object.
  • A command object could be sent across a network
    to be executed elsewhere or it could be saved as
    a log of operations.

16
Summary
  • Instead of this scenario
  • user initiates an event
  • event is sent to an event-handler (listener) and
    "consumed" leaving no trace
  • Events represented as high-level commands,
    actions, or transactions can also be stored in a
    collection, queued, stored as an object in a
    HashMap, saved to a file, or undone and redone

17
References
  • Adelson and Soloway B. Adelson and E. Soloway.
    The Role of Domain Experience in Software Design.
    IEEE Trans. on Software Engineering, V SE-11, N
    11, 1985, pp. 1351-1360.
  • Linn and Clancy M. Linn and M. Clancy, The Case
    for Case Studies of Programming Problems.
    Communications of the ACM V 35 N 3, March 1992,
    pp. 121-132.
  • Soloway and Ehrlich E. Soloway and K. Ehrlich,
    Empirical Studies of Programming Knowledge, IEEE
    Transactions on Software Engineering V SE-10, N
    5, September 1984.
  • Curtis B. Curtis, Cognitive Issues in Reusing
    Software Artifacts. In Software Reusability, V
    II. ed. T. Biggerstaff and A. Perlis, Addison
    Wesley 1989, pp. 269-287.
  • http//www.exciton.cs.rice.edu/JavaResources/Desig
    nPatterns/command.htm
Write a Comment
User Comments (0)
About PowerShow.com