Encapsulate a request or action as an object. Classification: - - PDF document

encapsulate a request or action as an object
SMART_READER_LITE
LIVE PREVIEW

Encapsulate a request or action as an object. Classification: - - PDF document

Department of Computer Science - COS 121 September 2012 COS 121 Christoph Stallmann 1 Encapsulate a request or action as an object. Classification: Behavioural Invoker Action 1 Action 2 Action 3 Receiver 2 Christoph


slide-1
SLIDE 1

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 1 COS 121 – Christoph Stallmann

1

  • Encapsulate a request or action as an
  • bject.
  • Classification: Behavioural

2

Receiver Action 1 Action 2 Action 3 Invoker

slide-2
SLIDE 2

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 2

  • Easy to add new commands.
  • Queue different commands and execute

them in order.

  • Log different actions.
  • Support undoable operations.

3 4

slide-3
SLIDE 3

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 3

  • Often abstract.
  • Defines an interface for executing a

command or action.

5

  • Defines a binding between the receiver

and an action.

  • Implements the execute function.
  • The execute() function calls the

Receiver’s action() function.

6

slide-4
SLIDE 4

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 4

  • Knows how to execute a specific action.
  • Any class can serve as a Receiver.

7

  • Asks the Command to carry out a

request.

8

slide-5
SLIDE 5

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 5

  • The application.
  • Creates the Concrete Command and

sets its receiver.

  • Often this responsibility is delegated

to the Invoker.

9

  • Initial state.

10

Command 1 Command 2 Command 3 Invoker Receiver

slide-6
SLIDE 6

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 6

  • Connect the Receiver to the Commands.

11

Command 1 Command 2 Command 3 Invoker Receiver

  • Commands are now connected to the

Receiver.

12

Invoker Receiver Command 1 Command 2 Command 3

slide-7
SLIDE 7

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 7

  • The Invoker changes or receives

external request.

13

Command 1 Command 2 Command 3 Invoker Receiver

  • The Invoker executes a specific

Command.

14

Command 1 Command 2 Command 3 Receiver Invoker

slide-8
SLIDE 8

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 8

  • The Command calls a specific action on

the receiver.

15

Command 1 Command 2 Command 3 Receiver Invoker

  • The Receiver executes a specific action.

16

Command 1 Command 2 Command 3 Receiver Invoker

slide-9
SLIDE 9

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 9

  • Depending on the request, the Invoker

might also execute a different command.

17

Command 1 Command 2 Command 3 Receiver Invoker

  • http://youtu.be/K1XeTF87Rgk

18

slide-10
SLIDE 10

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 10

19

Squad – Receiver Mouse – Invoker

20

Squad – Receiver Mouse – Invoker Select squad

slide-11
SLIDE 11

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 11

21

Squad – Receiver Mouse – Invoker

22

Squad – Receiver Mouse – Invoker Left mouse click

  • n the

screen

slide-12
SLIDE 12

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 12

23

Squad – Receiver Mouse – Invoker

24

Squad – Receiver Mouse – Invoker Mouse click on the grenade button

slide-13
SLIDE 13

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 13

25

Squad – Receiver Mouse – Invoker

26

Squad – Receiver Mouse – Invoker Mouse click

  • n the

retreat button

slide-14
SLIDE 14

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 14

27

Squad – Receiver Mouse – Invoker

28

slide-15
SLIDE 15

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 15

29 30

slide-16
SLIDE 16

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 16

  • Decoupling
  • Decouples the object that invokes an
  • peration from the one that performs

it.

  • Extensibility:
  • Commands can easily be

manipulated and extended.

  • Composition:
  • Assemble commands in more

complex composite/macro commands.

  • Addition:
  • Easy to add new Commands.

31

  • Intelligence:
  • How intelligent should a command

be?

  • One extreme is to delegate

everything to the Receiver, in the

  • ther extreme the Command does

everything.

  • Undo and redo operations:
  • Commands can undo/redo
  • perations.
  • Command must keep additional state

to handle these operations.

32

slide-17
SLIDE 17

Department of Computer Science - COS 121 September 2012 Christoph Stallmann - University of Pretoria 17

  • Chain od Responsibility:
  • Commands can server as request
  • bjects.
  • Composite:
  • To implement macro Commands.
  • Memento:
  • Keep state for undo operations.
  • Prototype:
  • Commands can be copied to be

place in a history list.

33

COS 121 – Christoph Stallmann

34