• Keine Ergebnisse gefunden

Design Patterns and Restructuring Command Oliver Haase

N/A
N/A
Protected

Academic year: 2022

Aktie "Design Patterns and Restructuring Command Oliver Haase"

Copied!
17
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Design Patterns and Restructuring

Command

Oliver Haase

HTWG Konstanz

(2)

Description

Classification: Object-based behavioral pattern

Purpose: Encapsulate a command as an object. Allows to

dynamically configure an invoker with a command object. Invoker can invoke command without knowing its specifics nor the receiver of the command.

Also known as: Action, Transaction

(3)

Motivation & Remarks

Most widely known application of the command pattern: event listeners for GUI programming

Remark: Command pattern is the object-oriented counterpart of function pointers in procedural languages

Remark II: If the command object contains only one operation, function pointers can be considered more lightweight and thus more appropriate

(4)

Remarks

Remark III: In C#, a function pointer is called a delegate. Example:

p u b l i c d e l e g a t e b o o l e a n FUNC( o b j e c t x , o b j e c t y ) ; p u b l i c b o o l e a n Compare ( o b j e c t x , o b j e c t y ) { . . . } p u b l i c b o o l e a n c o n t a i n s ( o b j e c t x , o b j e c t [ ] f i e l d ) {

CMP Func myFuncObj = new FUNC( Compare ) ;

f o r e a c h ( o b j e c t o b j i n f i e l d ) { i f ( myFuncObj ( o b j , x ) )

r e t u r n t r u e; }

r e t u r n f a l s e: }

IDIOM

(5)

Remarks

Remark III: In C#, a function pointer is called a delegate. Example:

p u b l i c d e l e g a t e b o o l e a n FUNC( o b j e c t x , o b j e c t y ) ; p u b l i c b o o l e a n Compare ( o b j e c t x , o b j e c t y ) { . . . } p u b l i c b o o l e a n c o n t a i n s ( o b j e c t x , o b j e c t [ ] f i e l d ) {

CMP Func myFuncObj = new FUNC( Compare ) ;

f o r e a c h ( o b j e c t o b j i n f i e l d ) { i f ( myFuncObj ( o b j , x ) )

r e t u r n t r u e; }

r e t u r n f a l s e: }

IDIOM

(6)

Motivation

In Java Swing, when a JButton is pressed, the button invokes the actionPerformedmethod of a pre-registered ActionListener command object.

TheActionListener interface contains only the actionPerformed method.

an ActionListener command object can be registered using the JButton’saddActionListener method.

(7)

Another Remark

Remark IV: In Java, an anonymous class might be a good choice to avoid the fully-fledged declaration of a class for a single method that is called only once:

J B u t t o n s u b m i t B u t t o n = new J B u t t o n ( ” s u b m i t ” ) ;

s u b m i t B u t t o n . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( ) { p u b l i c v o i d a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {

// do w h a t e v e r i s n e e d e d t o s u b m i t }

}) ;

IDIOM

(8)

Another Remark

Remark IV: In Java, an anonymous class might be a good choice to avoid the fully-fledged declaration of a class for a single method that is called only once:

J B u t t o n s u b m i t B u t t o n = new J B u t t o n ( ” s u b m i t ” ) ;

s u b m i t B u t t o n . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( ) { p u b l i c v o i d a c t i o n P e r f o r m e d ( A c t i o n E v e n t e ) {

// do w h a t e v e r i s n e e d e d t o s u b m i t }

}) ;

IDIOM

(9)

Sample Structure

(10)

Applicability

Use the command pattern if you want to

dynamically configure an object with an action;

you want to support →undofunctionality;

you want to keep a log of the executed actions so they can beredone in case of a crash;

model a complex transaction;

→ in this case the encapsulation of the transaction in a command object reduces code dependencies.

(11)

General Structure

(12)

Participants

Command: declares interface for the command invocation ConcreteCommand:

defines the action to be performed defines the receiver of the action

Client: creates aConcreteCommand–object and passes the receiver into it.

Invoker: invokes the command through theCommand interface Receiver: knows how to perform the action

(13)

Interactions

Client creates concrete command object and determines the receiver Invoker stores the concrete command object

Invoker executes a request by invoking the command object’s execute operation

If a command can be undone, the command object stores the receiver’s original state so it can be restored later.

The concrete command object implements the request by calling operation at the receiver object

(14)

Undo and Redo

How to implement chains of undos and redos:

Provide an additionalundo operation in the Commandinterface; In each ConcreteCommandclass, store the state before execution of theexecute operation; this may include:

all parameters for the action performed by the receiver

all state information of the receiver that might change due to the execution of the action (→mementopattern)

Receiver must allow command object to restore receiver’s original state

Client stores all executed commands in acommand history

→ depth of command history determines number of possible undo/redo steps

command objects are copied before insertion into command history

(15)

Undo and Redo

How to implement chains of undos and redos:

Provide an additionalundo operation in the Commandinterface;

In each ConcreteCommandclass, store the state before execution of theexecute operation; this may include:

all parameters for the action performed by the receiver

all state information of the receiver that might change due to the execution of the action (→mementopattern)

Receiver must allow command object to restore receiver’s original state

Client stores all executed commands in acommand history

→ depth of command history determines number of possible undo/redo steps

command objects are copied before insertion into command history

(16)

Consequences

Command pattern decouples the entity that invokes a request (Invoker) from the one that knows how to implement it (Command) Command objects can be manipulated (configured) and extended as any other object.

Command objects can be composed to build macro objects. For this purpose, the composite pattern can be employed.

New command objects can easily be created without modifying existing classes.

(17)

Related Patterns

Thecomposite pattern can be used to build macro commands (in which case theMacroCommandclass doesn’t point to a receiver object)

If a command needs to store the receiver’s state (undo), it can use the→memento pattern

If a command object needs to be copied before being inserted into the command history, then it behaves like aprototype

Referenzen

ÄHNLICHE DOKUMENTE

DOD has at various points said that theater cyber will be “under the command and control of which- ever combatant command to which they are assigned,” will be “aligned under one

I Receiver must allow command object to restore receiver’s original state. I Client stores all executed commands in a

A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior..

during movement of the movable member past each of said incremental positions, integrating means con nected to said differentiating means for integrating said velocity signal

(i.e., alphanumerics, rectangles, figure placement, etc.). Commarrl primitives are specified to the driver by the application program via the comnand channel

Portuguese) 26 0 Abicomp (Braz.. Portuguese) 26 0

Set breakpoint at address. The breakpoint is executed count-1 times before causing a stop. Each time the breakpoint is encountered, the command c is executed. If this

or search permission is denied on a component of the path prefix. If the file is located on a remote host, this error code will be returned if the local host name