POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A....
Transcript of POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Command pattern | Tri A....
POLA-POLA
PERANCANGAN (PPP)
Behavioral pattern: Command
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 2/20
Tujuan perkuliahan
• Memahami behavioral pattern: Command
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
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?
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
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 6/20
Motivation (2) – remote control
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
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)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 9/20
Structure
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
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
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
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 13/20
Sample code
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 14/20
Sample code (1)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 15/20
Sample code (2)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 16/20
Sample code (3)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 17/20
Sample code (4)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 18/20
Sample code (1)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 19/20
Sample code (6)
Bahan Kuliah PPP - Command pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 20/20
Sample code (7)