Download presentation
Presentation is loading. Please wait.
Published byEugene Craig Modified over 9 years ago
1
Command Pattern 1
2
Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request, and support undoable operations 2
3
Command Pattern An object is used to represent and encapsulate all the information needed to call a method at a later time This information includes the method name, the object that owns the method, and the values for the method parameters Three fundamental Command Pattern terms: Client : instantiates the Command Object and provides information to call the method at a later time Invoker : decides which method should be called (Executes the commands possibly at a later time) Receiver : an instance of the class that contains the method’s code (the object the command should affect) 3
4
Class Diagram 4 ClientInvoker setCommand () Receiver action() ConcreteCommand execute() undo () > Command execute() undo () Receiver.Action ()
5
Collaborations The Client is responsible for creating a ConcreteCommand and setting its Receiver The Receiver knows how to perform the work needed to carry out the request. Any class can act as a Receiver. The Invoker holds a ConcreteCommand and at some point asks the ConcreteCommand to carry out a request by calling its execute () method Command declares an interface for all commands The ConcreteCommand defines a binding between an action and a Receiver. The Invoker makes a request by calling execute () and the ConcreteCommand carries it out by calling one or more actions on the Receiver. 5
6
Sequence Diagram 6 A ReceiverA ClientA CommandAn Invoker new Command (Receiver) StoreCommand (Command) Execute () Action ()
7
Implementation (Command) public interface ICommand { public abstract void execute (); } 7
8
Implementation (invoke) class RemoteSwitch { private ICommand onCommand; private ICommand offCommand; public Switch (ICommand Up, ICommand Down) { onCommand = Up; offCommand = Down; } public void on () { onCommand.execute (); } public void off () { offCommand.execute (); } } 8
9
Implementation (receiver [fan]) class Fan { public void startRotate () { System.Debug.WriteLine (“Fan is rotating”); } public void stopRotate () { System.Debug.WriteLine (“Fan is not rotating”); } 9
10
Implementation (receiver [light]) class Light { public void turnOn() { System.Debug.WriteLine (“Light is on”); } public void turnOff() { System.Debug.WriteLine (“Light is off”); } 10
11
Implementation (ConcreteCommand) class LightOnCommand : ICommand { private Light myLight; public LightOnCommand (Light L) { myLight = L; } public void execute() { myLight.turnOn (); } } class LightOffCommand : ICommand { private Light myLight; public LightOffCommand (Light L) { myLight = L; } public void execute() { myLight.turnOff (); } } 11
12
Implementation (ConcreteCommand) class FanOnCommand : ICommand { private Fan myFan; public FanOnCommand (Fan F) { myFan = F; } public void execute() { myFan.startRotate (); } } class FanOffCommand : ICommand { private Fan myFan; public FanOffCommand (Fan F) { myFan = F; } public void execute() { myFan.stopRotate (); } } 12
13
Implementation (client) public class TestCommand { public static void Main (String[] args) { Light light = new Light (); LightOnCommand lightOn = new LightOnCommand (light); LightOffCommand lightOff = new LightOffCommand (light); RemoteSwitch remoteSwitch = new RemoteSwitch (lightOn, lightOff); remoteSwitch.on (); remoteSwitch.off (); Fan fan = new Fan(); FanOnCommand fanOn = new FanOnCommand (fan); FanOffCommand fanOff = new FanOffCommand (fan); RemoteSwitch remoteSwitch = new RemoteSwitch (fanOn, fanOff); remoteSwitch.on (); remoteSwitch.off (); } 13
14
Class Diagram 14 Client Switch (invoker) on() off() Light (Receiver) turnOn() turnOff() LightOnCommand execute() > Command execute() (Application) myLight.turnOn()
15
Main Concepts It decouples an object making a request, from the one that knows how to perform it The command requester only needs to know how to issue it, it doesn’t need to know how to perform it Command object is at the center of this decoupling and encapsulates a receiver with an action An invoker makes a request of a Command object by calling its execute() method, which invokes those actions on the receiver 15
17
References 17 Hyder, Shahriar. “Command Design Pattern”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.