Command Design Pattern - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Command Design Pattern

Description:

By definition, patterns capture experience. 3. OO Design Patterns. Patterns reduce. discovery costs ... UML View of the Sample. 9 // Command design pattern ... – PowerPoint PPT presentation

Number of Views:256
Avg rating:3.0/5.0
Slides: 15
Provided by: rickm1
Category:

less

Transcript and Presenter's Notes

Title: Command Design Pattern


1
Command Design Pattern
  • Rick Mercer

2
OO Design Patterns
  • It has long been recognized that expert
    programmers don't think about programs in terms
    of low level programming language elements, but
    in higher-order abstractions Adelson and
    Soloway Soloway and Erlich Curtis Linn and
    Clancy.
  • Patterns facilitate communication of OO designs
  • provide tried and true design solutions
  • are examples of good OO design
  • can be applied in different contexts
  • By definition, patterns capture experience

3
OO Design Patterns
  • Patterns reduce
  • discovery costs
  • design complexity
  • reinventing the wheel
  • A Design Pattern is a solution to a recurring
    problem, occurring in different contexts, often
    balancing competing forces

4
Command Pattern in Java
  • A component can issue requests to objects without
    knowing anything about the actual operation or
    the class of object
  • Can you think of an example?
  • Polymorphism lets you encapsulate a request as an
    object
  • Establish a method signature name one way to do
    this
  • Vary the algorithms in the called methods

5
Java Example
  • Java uses the Command pattern by having a
    listener object for each component
  • Different way to implement the same pattern
  • Sun used the Command pattern to improve the event
    model in Java 1.1
  • method signature
  • public void actionPerfomed(ActionEvent e)
  • JButtons an JTextFields send actionPerformed
    messages without knowing what will happen
  • Nor do they care

6
Command
  • The Command object can also be used when you need
    to tell the program to execute the command when
    the resources are available rather than
    immediately.
  • In such cases, you are saving commands as objects
    to be executed later

7
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)
  • WorkCommand command new DomesticEngineer()
  • objectToBytes.writeObject(command) // log in a
    file

What pattern is being applied here?
8
A UML View of the Sample
9
  • // 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")

10
  • public class SaveCommands
  • public static void main(String args) throws
    Exception
  • 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 the list to save for later use
  • ArrayList allCommands new ArrayList()
  • allCommands.add(commandWork)
  • allCommands.add(commandProg)
  • allCommands.add(commandWork)
  • allCommands.add(commandPol)
  • allCommands.add(commandWork)
  • allCommands.add(commandPol)

11
  • public class ExecuteSavedCommandObjects
  • public static void main(String args)
  • String fileName "logfile"
  • FileInputStream diskToStreamOfBytes null
  • ObjectInputStream bytesToObjects null
  • List list null
  • try
  • diskToStreamOfBytes new
    FileInputStream(fileName)
  • bytesToObjects new ObjectInputStream(diskToSt
    reamOfBytes)
  • list (ArrayList) bytesToObjects.readObject
    ()
  • bytesToObjects.close()
  • catch (FileNotFoundException e)
  • e.printStackTrace()
  • catch (IOException e)
  • e.printStackTrace()
  • catch (ClassNotFoundException e)
  • e.printStackTrace()

12
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.

13
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

14
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.
Write a Comment
User Comments (0)
About PowerShow.com