Presentation is loading. Please wait.

Presentation is loading. Please wait.

Command Pattern 1.

Similar presentations


Presentation on theme: "Command Pattern 1."— Presentation transcript:

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

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)

4 Class Diagram <<interface>> Command Client Invoker
execute() undo () setCommand () Receiver ConcreteCommand action() 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.

6 Sequence Diagram A Receiver A Client A Command An Invoker
new Command (Receiver) StoreCommand (Command) Execute () Action ()

7 Class Diagram <<interface>> Command Client execute()
Switch (invoker) Client on() off() execute() (Application) Light (Receiver) LightOnCommand turnOn() turnOff() execute() myLight.turnOn()

8 Implementation (Command)
public interface ICommand { void execute (); }

9 Implementation (invoke)
public class RemoteSwitch { private ICommand onCommand; private ICommand offCommand; public RemoteSwitch (ICommand Up, ICommand Down) onCommand = Up; offCommand = Down; } public void on () { onCommand.execute (); } public void off () { offCommand.execute (); }

10 Implementation (receiver [light])
public class Light { public void turnOn() System.Debug.WriteLine (“Light is on”); } public void turnOff() System.Debug.WriteLine (“Light is off”);

11 Implementation (ConcreteCommand)
public class LightOnCommand : ICommand { private Light myLight; public LightOnCommand (Light L) { myLight = L; } public void execute() { myLight.turnOn (); } } public class LightOffCommand : ICommand public LightOffCommand (Light L) { myLight.turnOff (); }

12 Implementation (receiver [fan])
public class Fan { public void startRotate () System.Debug.WriteLine (“Fan is rotating”); } public void stopRotate () System.Debug.WriteLine (“Fan is not rotating”);

13 Implementation (ConcreteCommand)
class FanOnCommand : ICommand { private Fan myFan; public FanOnCommand (Fan F) { myFan = F; } public void execute() { myFan.startRotate (); } } class FanOffCommand : ICommand public FanOffCommand (Fan F) { myFan.stopRotate (); }

14 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 remoteSwitch1 = new RemoteSwitch (lightOn, lightOff); remoteSwitch1.on (); remoteSwitch1.off (); Fan fan = new Fan(); FanOnCommand fanOn = new FanOnCommand (fan); FanOffCommand fanOff = new FanOffCommand (fan); RemoteSwitch remoteSwitch2 = new RemoteSwitch (fanOn, fanOff); remoteSwitch2.on (); remoteSwitch2.off (); }

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

16 ?

17 References Hyder, Shahriar. “Command Design Pattern”


Download ppt "Command Pattern 1."

Similar presentations


Ads by Google