Command Design Pattern - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Command Design Pattern

Description:

By definition, patterns capture experience. 21-3. OO Design ... A UML View of the Sample. 21-9 // Command design pattern - Decoupling producer from consumer ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 14
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 as follows
  • Establish a method signature
  • Name two ways to do this
  • vary the effect of the calling the method

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)
  • Buttons send actionPerformed messages without
    knowing what will happen

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

8
A UML View of the Sample
9
  • // Command design pattern - Decoupling producer
    from consumer
  • 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.println("take money from the rich,
    take votes from the poor")

10
  • import java.io.
  • import javax.swing.JOptionPane
  • public class CommandPattern
  • public static void main(String args) throws
    Exception
  • String fileName "logfile"
  • FileOutputStream bytesToDisk new
    FileOutputStream(fileName)
  • ObjectOutputStream objectToBytes new
    ObjectOutputStream(bytesToDisk)
  • // Imagine each command is some important
    transaction
  • // that is logged before it is executed
  • WorkCommand command new DomesticEngineer()
  • objectToBytes.writeObject(command)
  • command new Programmer()
  • objectToBytes.writeObject(command)
  • command new Politician()

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

12
Summary
  • Instead of an event just being generated, sent to
    an event-handler (listener) and "consumed"
    leaving no trace, events (represented as
    high-level commands, actions, or transactions)
    can be stored in a Vector, queued, stored as the
    object in a HashMap, save to a file, or undone
    and redone

13
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