Download presentation
Presentation is loading. Please wait.
1
Programming Design Patterns
Remote Control Suppose you have a remote control with 7 slots Each slot has an ON and OFF button The remote controls a Light Bulb and a Garage Opener Code can be loaded into each slot the code for each slot can be changed from time to time When a button is pressed, assume a method “buttonWasPressed” is invoked on the remote with the slot number passed in as a parameter Code for the Light bulb can turn it ON and OFF Code for the Garage Opener can open and close it. Design code for the Remote Control, Light Bulb, and Garage Opener Programming Design Patterns 11/8/2018 1 1
2
Programming Design Patterns
Design Considerations Requester of the action: Remote Control Performer of the action: Light Bulb, Garage Door Decouple the requester from the action performer Programming Design Patterns 11/8/2018 2 2
3
Programming Design Patterns
Code Design (1) Store a command object for each button The remote control should have no idea what the work is for each slot Note that the functionality of each slot can be re- programmed Remote control should know how to “talk” to the object loaded in a slot talk to object in the slot in the same way each time Programming Design Patterns 11/8/2018 3 3
4
Programming Design Patterns
Code Design (2) Need standard API to allow code to be added to each slot should be able to call “setCommand()” on the Remote Control All the objects controlled by the Remote Control should have a common method execute() should implement the “Command Interface” Programming Design Patterns 11/8/2018 4 4
5
Programming Design Patterns
Command Interface public interface Command { public void execute(); } Programming Design Patterns 11/8/2018 5 5
6
Programming Design Patterns
LightOn Command public class LightOnCommand implements Command { // CLASSWORK // constructor that takes a Light object // implement the interface method and accordingly // invoke a method on the Light object } Programming Design Patterns 11/8/2018 6 6
7
Programming Design Patterns
LightOn Command public class LightOnCommand implements Command { private Light light; public LightOnCommand(Light lightIn) { light = LightIn; } // execute calls specific methods on // the receiving object public void execute() { light.on(); Programming Design Patterns 11/8/2018 7 7
8
Programming Design Patterns
SimpleRemoteControl public class SimpleRemoteControl { // CLASSWORK // store all the commands // code for buttonWasPressed() } Programming Design Patterns 11/8/2018 8 8
9
Programming Design Patterns
SimpleRemoteControl public class SimpleRemoteControl { private Command slot; public SimpleRemoteControl() {..} public void setCommand(Command commandIn) { slot = commandIn; } // Note how the remote control is decoupled from // the object that performs the action. Execute is // called for all the devices that are contoled public void buttonWasPressed() { slot.execute(); Programming Design Patterns 11/8/2018 9 9
10
Programming Design Patterns
RemoteControl with many slots CLASSWORK Extend code for RemoteControl to support multiple slots Programming Design Patterns 11/8/2018 10 10
11
Programming Design Patterns
RemoteControl with many slots Extend code for RemoteControl to support multiple slots public class RemoteControl { Command[] onCommands; Command[] offCommands; public void onButtonWasPushed(int slot) { onCommands[slot].execute(); } // … Programming Design Patterns 11/8/2018 11 11
12
Programming Design Patterns
Command Pattern: Definition Encapsulates a request as an object, thereby letting you to parameterize other objects with different requests, queue or log requests, and support undoable operations what does “thereby letting you parameterize other objects with different requests” mean? Homework How can you “log requests”? Undo operations: support the “undo” command Constructor is passed different parameters Log requests: use a queue Programming Design Patterns 11/8/2018 12 12
13
Programming Design Patterns
Undo and MetaCommand How can you add code for an “undo” operation? How can you implement the Meta Command Operation? multiple commands with a single button on the remote CLASSWORK Programming Design Patterns 11/8/2018 13 13
14
Programming Design Patterns
Undo public interface Command { public void execute(); public void undo(); } Should keep track of the last command in the RemoteControl a data member Command undoCommand; Write code for “undo” for LightOnCommand object Programming Design Patterns 11/8/2018 14 14
15
Programming Design Patterns
LightOn Command public class LightOnCommand implements Command { Light light; public LightOnCommand(Light lightIn) { light = LightIn; } // execute calls specific methods on // the receiving object public void execute() { light.on(); public void undo() { light.off(); Programming Design Patterns 11/8/2018 15 15
16
Programming Design Patterns
History of Undo? How can you implement a history of Undo operations? Homework? Use a stack to keep track of the executed commands Programming Design Patterns 11/8/2018 16 16
17
Programming Design Patterns
Execute a series of steps? public void execute() { stereo.on(); stereo.setCD(); stereo.setVolume(); } Programming Design Patterns 11/8/2018 17 17
18
Programming Design Patterns
Error handling What if a command is not loaded in a slot? Write code for slots in which an appropriate object may not be loaded (or perhaps the command object for that slot is null) CLASSWORK Programming Design Patterns 11/8/2018 18 18
19
Programming Design Patterns
Null Command (2) public void onButtonWasPressed(int slot) { if (onCommands[slot] != null) { onCommands[slot].execute(); } With this implementation approach client is forced to handle cases with an “if” loop Programming Design Patterns 11/8/2018 19 19
20
Programming Design Patterns
Null Command (3) public class NoCommand implements Command { public void execute() { } Load the Null Command onto empty slots NoCommand: example of a null object Removes the responsibility from the client to handle null cases Null Object is sometimes listed as a design pattern Programming Design Patterns 11/8/2018 20 20
21
Programming Design Patterns
Homework Page 228 in the book queuing requests This idea is used in and Java Spaces Programming Design Patterns 11/8/2018 21 21
22
Programming Design Patterns
Problem Statement Suppose you have a set of worker computers that donate free CPU cycles to solve problems A problem can be broken into various parts, each of which can be independently solved by any of the free worker-computers. The various parts of the problem can be dropped off as objects into a shared space (think of it as a queue). Design code for this “Shared-Problem-Space” scenario. (similar to Java Spaces) Programming Design Patterns 11/8/2018 22 22
23
Programming Design Patterns
Command Pattern Encapsulate a request as an object letting you parameterize clients with different requests Homework Queue or log requests, Support undoable operations. Programming Design Patterns 11/8/2018 23 23
24
Programming Design Patterns
Command Pattern: features Can issue requests to objects without knowing anything about the operation being requested or the receiver of the request. Decouples the object that invokes the operation from the one that knows how to perform it. The action class contains an execute() method that simply calls the action on the receiver. All clients of Command objects treat each object as a "black box" by simply invoking the object's execute() method whenever the client requires the object's "service". Programming Design Patterns 11/8/2018 24 24
25
Programming Design Patterns
Command Pattern Features (2) Sequences of Command objects can be assembled into composite (or macro) commands. Programming Design Patterns 11/8/2018 25 25
26
Programming Design Patterns
Check List for Command Pattern Define a Command interface public void execute(). Create one or more derived classes that encapsulate some subset of the following: a "receiver" object, the method to invoke, the arguments to pass. Instantiate a Command object for each deferred execution request. Pass the Command object from the creator (sender) to the invoker (receiver). The invoker decides when to execute(). Programming Design Patterns 11/8/2018 26 26
27
Programming Design Patterns
1. Document Editor Menus In a document editor, there are often lots of pull-down menus, containing lots of menu items. At any given time during the editing process, some of these are enabled, and some disabled. For example, an "Edit" pull-down menu may have menu items for "Undo Clear", "Redo Paste", "Cut", "Copy", "Paste", and "Paste as Hyperlink". Design a simple menu- driven editor that allows you to choose different pull-down menus, and shows options for the ones that are enabled and ones that are disabled. Specific methods should be called whenever a menu option is selected. However, the method implementation should just print a statement saying that it was called. Programming Design Patterns 11/8/2018 27 27
28
Programming Design Patterns
2. University Admissions Office The admissions office of a university keeps online documentation in a single format. It gets updated frequently and the single format helps maintenance (make changes in only one place). However, the new requirement is that the users should be able to download information in different formats such as HTML, PDF, and Word. Assume that as a Computer Science major you are aware of open source packages that convert the information to these formats. Programming Design Patterns 11/8/2018 28 28
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.