Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,

Similar presentations


Presentation on theme: "1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,"— Presentation transcript:

1 1 Command Design Pattern Rick Mercer

2 Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands, send commands over the internet, support undo and redo, have listeners wait to execute their code, …. http://en.wikipedia.org/wiki/Command_pattern 2

3 Example from Heads First RemoteLoader loads commands into slots of the remote control 3

4 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 as an interface or abstract class Vary the algorithms in the called methods

5 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 Mouse clicked and moved events make something happen in method mouseClicked and mouseMoved Event generators — buttons, text fields, mouse — have a list listener objects Though add listener methods

6 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 Stack for undo/redo operations

7 7 Example: 3 Command objects

8 8 public abstract class Worker implements Serializable { protected BankAccount myAccount; public double getBalance() { return myAccount.getBalance(); } public abstract void justDoIt(double pay); } class DomesticEngineer extends Worker { public DomesticEngineer(String name) { myAccount = new BankAccount(name, 0.0); } public void justDoIt(double pay) { myAccount.deposit(pay); System.out.println("Take out the trash"); } class Politician extends Worker { //...

9 9 Create Workers and save In the Eclipse project, SaveWorkCommands will save instances to a file Worker dev = new Programmer("Casey"); Like RemoteLoader in HFSP or Client in gen. form When we want them to do work and get paid, run ReadAndExecuteSavedCommands dev.justDoIt(20.00); Like Light in HFSP or Receiver in gen. form

10 10 Structure of Command The client that creates a command is not the same code that executes it This separation provides flexibility in the timing and sequencing of commands Materializing commands as objects means they can be passed, staged, shared, loaded in a table, and otherwise instrumented or manipulated like any other object

11 11 Structure of Command 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 12 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/comman d.htm


Download ppt "1 Command Design Pattern Rick Mercer. Command Design Pattern The Command Pattern encapsulates a request as an object, thereby letting you queue commands,"

Similar presentations


Ads by Google