1 / 11

Command Design Pattern

Command Design Pattern. Rick Mercer. Command Design Pattern. The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue commands, send commands over the internet like in a networked game, and support undo and redo….

bluma
Download Presentation

Command Design Pattern

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Command Design Pattern Rick Mercer

  2. Command Design Pattern • The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue commands, send commands over the internet like in a networked game, and support undo and redo…. http://en.wikipedia.org/wiki/Command_pattern

  3. Example for Heads FirstNote: RemoteLoader loads commands into slots of the remote control

  4. Command Pattern • 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

  5. 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 listeners) without knowing what will happen • Event generators — buttons, text fields, mouse — have listener objects (actually a List of listener objects)

  6. 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 also sending command objects over the network (in new project) or save them in a collection class such as a Stack for undo/redo operations

  7. Example we saw before was Command • Make 3 command classes • Log instances by writing the objects to a file • See SaveWorkCommands .java in the Eclipse project CommandPattern (command.zip) • Like RemoteLoader in HFSP or Client in gen. form • Read the objects later and execute them • See ExecuteSavedCommands .java in the Eclipse project CommandPattern (command.zip) • Like Light on HFSP or Receiver in gen. form

  8. A UML View of the Sample

  9. import java.io.Serializable; // Command design pattern - Decoupling producer from consumer. publicinterface WorkCommand { void execute(); } class DomesticEngineer implements WorkCommand, Serializable { publicvoid execute() { System.out.println("Take out the trash."); } } class Politician implements WorkCommand, Serializable { publicvoid execute() { System.out.println("Take money from the rich, take votes from the poor."); } } class Programmer implements WorkCommand, Serializable { publicvoid execute() { System.out.println("Sell the bugs, charge extra for the fixes."); } } See code demos page for CommandPattern.zip

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

  11. 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. • [Sierra and Bates ], Heads First Design Patterns • http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/command.htm

More Related