command pattern 1. intent encapsulates a request as an object, thereby letting you parameterize...

17
Command Pattern 1

Upload: eugene-craig

Post on 18-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Command Pattern

1

IntentEncapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request, and support undoable operations

2

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

Class Diagram

4

Client Invoker

setCommand ()

Receiver

action()

ConcreteCommand

execute()undo ()

<<interface>>Command

execute()undo ()

Receiver.Action ()

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

Sequence Diagram

6

A Receiver A Client A Command An Invoker

new Command (Receiver)

StoreCommand (Command)

Execute ()

Action ()

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

7

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

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

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

10

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

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

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

Class Diagram

14

ClientSwitch

(invoker)on()off()

Light(Receiver)

turnOn()turnOff()

LightOnCommand

execute()

<<interface>>Command

execute()(Application)

myLight.turnOn()

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

?

References

17

• Hyder, Shahriar. “Command Design Pattern”