Lernkarten

Karten 7 Karten
Lernende 1 Lernende
Sprache Deutsch
Stufe Universität
Erstellt / Aktualisiert 28.12.2019 / 11.01.2020
Lizenzierung Keine Angabe
Weblink
Einbinden
0 Exakte Antworten 7 Text Antworten 0 Multiple Choice Antworten
Fenster schliessen

The Command Pattern (Definition)

Lizenzierung: Keine Angabe

The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

Fenster schliessen

Class Diagram of Command Pattern

Lizenzierung: Keine Angabe

Client: The Client is responsible for creating a ConcreteCommand and setting its Receiver (=Light in example) (testSimpleRemoteControl in example).

Invoker: The Invoker holds a command and at some point asks the command to carry out a request by calling its execute() method (SimpleRemoteControl in example).

Receiver: The Receiver knows how to perform the work needed to carry out the request (Light in example). Any class can act as a Receiver. 

Command: Command declares an interface for all commands. As you already know, a command is invoked through its execute() method, which asks a receiver to perform an action (Command interface in example).

ConcreteCommand: The ConcreteCommand (LightCommandOn in example) defines a binding between an action and a Receiver. The Invoker makes a request by calling execute() and the ConcreteCommand carries it out by calling one or more actions on the Receiver.

Fenster schliessen

Command Pattern with Undo Code Example

Lizenzierung: Keine Angabe
Fenster schliessen

Command Pattern Queuing Requests

Lizenzierung: Keine Angabe
Fenster schliessen

Command Pattern Logging Requests

Lizenzierung: Keine Angabe
Fenster schliessen

The State Pattern (Definition)

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Fenster schliessen

Class Diagram of State Pattern

Lizenzierung: Keine Angabe

Context: The Context is the class that can have a number of internal states. In our example, the GumballMachine is the Context.

Whenever the request() is made on the Context it is delegated to the state to handle.

State: The State interface defines a common interface for all concrete states; the states all implement the same interface, so they are interchangeable.

ConcreteStates: ConcreteStates handle requests from the Context. Each ConcreteState provides its own implementation for a request. In this way, when the Context changes state, its behavior will change as well.