Command (design pattern)

from Wikipedia, the free encyclopedia

In the object-oriented programming is command (also command ; English command ) is a design pattern that the category of behavioral patterns (English behavioral design patterns ) belongs. In this design pattern, the command object encapsulates a command in order to enable operations to be queued , log entries to be made and operations to be undone. It's one of the GoF patterns.

use

If z. For example, if a button in a graphical user interface is to be linked to an action, the command is used to parameterize the action to be carried out. Thus, the object-oriented correspondence to the callback functions ( callback function ) is. Here, the creation of the command and the actual execution at different times or in a different context (thread, process, machine) can take place.

Implementation of an undo mechanism ( undo ) : With each execution, the data necessary for the reversal are saved in the object and the object itself is saved on a stack. To the contrary, restoring ( redo ) to implement a second stack for the undone commands sufficient.

actors

UML class diagram of the command

The command is the base class of all commands. A specific command saves the status required for execution, typically including a reference to the recipient, and implements the command interface.

The client generates a specific command and provides it with a reference to the recipient and all other necessary information. It gives the caller a reference to the specific command .

The caller has one or more references to commands and, if necessary, requests them to perform their action. There are no special requirements for the recipient . He doesn't have to know anything about the other actors. Thus, each class can serve as a recipient . The concrete command calls methods of the recipient object to carry out its action.

Advantages disadvantages

The initiator and the executor are decoupled. Command objects can also be manipulated like other objects (change, filter, buffer, ...). Command objects can be combined to form complex commands ( macros , implemented as a compound word ).

Since a new class is required for each command, the number of these can quickly become large and the implementation confusing.

Example in PHP

abstract class Kommando {
    abstract function ausfuehren();
}

class Aufrufer {
    private $history = array();

    public function speichernUndAusfuehren(Kommando $cmd) {
        $this->history[] = $cmd; // optional
        $cmd->ausfuehren();
    }
}
 
// Empfänger
class Licht {
    public function licht_an() {
        write_line('Licht ist an.');
    }
    public function licht_aus() {
        write_line('Licht ist aus.');
    }
}
 
// konkretes Kommando #1: Licht an
class Kommando_An extends Kommando {
    private $dasLicht;
    public function __construct(Licht $licht) {
        $this->dasLicht = $licht;
    }
    public function ausfuehren() {
        $this->dasLicht->licht_an();
    }
}

// konkretes Kommando #2: Licht aus
class Kommando_Aus extends Kommando {
    private $dasLicht;
    public function __construct(Licht $licht) {
        $this->dasLicht = $licht;
    }
    public function ausfuehren() {
        $this->dasLicht->licht_aus();
    }
}

// Der Klient
function Test($kommando_string) {
    $lamp     = new Licht();
    $kmd_an   = new Kommando_An ($lamp);
    $kmd_aus  = new Kommando_Aus($lamp);

    $aufrufer = new Aufrufer();

    switch ($kommando_string) {
        case 'ON':
            $aufrufer->speichernUndAusfuehren($kmd_an);
        break;
        case 'OFF':
            $aufrufer->speichernUndAusfuehren($kmd_aus);
        break;
        default:
            write_line('Nur die Argumente "ON" oder "OFF" sind erlaubt.');
    }
}

function write_line($text) {
    print $text.'<br/>';
}

Test('ON');
Test('OFF');

Example in TypeScript

 1 enum COM{
 2     ON,
 3     OFF
 4 }
 5 
 6 abstract class Kommando {
 7     ausfuehren() : void {
 8 
 9     };
10 }
11 
12 class Aufrufer {
13     private history = [];
14 
15     speichernUndAusfuehren(cmd: Kommando) : void {
16         this.history.push(cmd); // optional
17         cmd.ausfuehren();
18     }
19 }
20 
21 // Empfänger
22 class Licht {
23     constructor(){}
24     licht_an() : void {
25         write_line('Licht ist an.');
26     }
27     licht_aus() : void {
28         write_line('Licht ist aus.');
29     }
30 }
31 
32 // konkretes Kommando #1: Licht an
33 class Kommando_An extends Kommando {
34     private dasLicht : Licht;
35     constructor(licht: Licht) {
36         super();
37         this.dasLicht = <Licht> licht;
38     }
39     ausfuehren() : void {
40         this.dasLicht.licht_an();
41     }
42 }
43 
44 // konkretes Kommando #2: Licht aus
45 class Kommando_Aus extends Kommando {
46     private dasLicht: Licht;
47     constructor(licht: Licht) {
48         super();
49         this.dasLicht = <Licht> licht;
50     }
51     ausfuehren() : void {
52         this.dasLicht.licht_aus();
53     }
54 }
55 
56 // Der Klient
57 function Test(kommando_string : string|number) : void {
58     const lamp : Licht     = new Licht();
59     const kmd_an : Kommando  = new Kommando_An (lamp);
60     const kmd_aus: Kommando  = new Kommando_Aus(lamp);
61 
62     const aufrufer: Aufrufer = new Aufrufer();
63 
64     switch (kommando_string) {
65         case 1:
66         case 'ON':
67             aufrufer.speichernUndAusfuehren(kmd_an);
68             break;
69         case 0:
70         case 'OFF':
71             aufrufer.speichernUndAusfuehren(kmd_aus);
72             break;
73         default:
74             write_line('Nur die Argumente "ON" oder "OFF" sind erlaubt.');
75     }
76 }
77 
78 function write_line(text: string) {
79     console.log(text);
80 }
81 
82 Test('ON');
83 Test('OFF');
84 Test(COM.ON);
85 Test(COM.OFF);

Output:

Licht ist an.
Licht ist aus.

Web links

Commons : command (design pattern)  - collection of images, videos and audio files
Wikibooks: Pattern: Command  - Learning and teaching materials

Individual evidence

  1. Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides : Design pattern . 5th edition. Addison-Wesley , 1996, ISBN 3-8273-1862-9 , pp. 287 .