POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A....

20
POLA-POLA PERANCANGAN (PPP) Behavioral pattern: Command

Transcript of POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A....

Page 1: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

POLA-POLA

PERANCANGAN (PPP)

Behavioral pattern: Command

Page 2: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 2/20

Tujuan perkuliahan

• Memahami behavioral pattern: Command

Page 3: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/20

Intent and Known As• Intent:

– Encapsulate a request as an object, thereby

letting you parameterize clients with different

requests, queue or log requests, and support

undoable operations

• Know As:

– Action, Transaction

Page 4: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/20

Motivation

• Bad event-handling code what if there is

a new event?

Page 5: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 5/20

Motivation (1)

• Common UI commands ensure the

code implementing these common

commands is not duplicated

Page 6: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 6/20

Motivation (2) – remote control

Page 7: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 7/20

Motivation (3) – goals

• Separate the remote’s ability to execute a

command from the variety of operations

and devices that can be supported

• What varies?

– Devices and operations

• What stays the same?

– The structure of the remote control

Page 8: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 8/20

Motivation (4) – parts

• The actions that we want each button to

cause (Command)

• Someone to set up those actions (Client)

• The remote control to execute the actions

(Invoker)

• The device affected by the action

(Receiver)

Page 9: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 9/20

Structure

Page 10: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20

Participants

• Command– declares an interface for executing an operation

• ConcreteCommand– defines a binding between a Receiver object and an action

– implements Execute by invoking the corresponding operation(s) on Receiver

• Invoker– asks the command to carry out the request

• Receiver– knows how to perform the operations associated with carrying

out a request

– Any class may serve as a Receiver

Page 11: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 11/20

Collaborations

• The client creates a ConcreteCommand object and specifies its Receiver

• An Invoker object stores the ConcreteCommand object

• The Invoker issues a request by calling Execute() on the Command. When commands are undoable, ConcreteCommand stores state for undoing the command prior to invoking Execute().

• The ConcreteCommand object invokes operations on its Receiver to carry out the request

Page 12: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 12/20

Consequences

• Command decouples the object that invokes the operation from the one that knows how to perform it

• Commands are first-class objects. They can be manipulated and extended like any other object.

• You can assemble commands into a composite command. In general, composite commands are an instance of the Composite pattern.

• It's easy to add new commands, because you don't have to change existing classes

Page 13: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 13/20

Sample code

Page 14: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 14/20

Sample code (1)

Page 15: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 15/20

Sample code (2)

Page 16: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 16/20

Sample code (3)

Page 17: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 17/20

Sample code (4)

Page 18: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 18/20

Sample code (1)

Page 19: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 19/20

Sample code (6)

Page 20: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/20 Participants • Command –declares an interface for executing

Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 20/20

Sample code (7)