170 likes | 308 Views
This document explores the implementation of the Command Pattern in Java, focusing on managing remote control operations. It demonstrates how commands can be decoupled from their execution using interchangeable button commands. The example includes classes for Light control and an EntranceGate, illustrating the creation and execution of commands. Furthermore, it highlights a SimpleRemoteControl that can execute commands upon button presses, using a test scenario to validate functionality. The material also covers diagrammatic representation and additional functions, like "undo".
E N D
Das Command Muster Deimbacher, Gölles
Fakten • Verhaltensmuster • GoF – Muster • Auslösender und Ausführender sind entkoppelt.
Die Fernbedienung ON-Knopf 1 OFF-Knopf 1 Rückgängig Knopf
Austauschbare Befehle • Die Geräte die gesteuert werden sind verschieden
Command publicinterface Command { publicvoidexecute(); }
Licht an… publicclassLightOnCommandextends Command { private Light light; publicLightOnCommand(Light light) { this.light = light; } publicvoidexecute() { light.on(); } } Bei Ausführung wird Licht eingeschalten
Fernbedienung • publicclassSimpleRemoteControl{ • private Command slot; • publicSimpleRemoteControl() {} • publicvoidsetCommand(Command command) { • slot = command; • } • publicvoidbuttonWasPressed() • { • slot.execute(); • } • }
Testen der Fernbedienung • public class RemoteControlTest { • publicstaticvoid main(String[] args) { • SimpleRemoteControlremote = newSimpleRemoteControl(); • Light light = new Light(); • LightOnCommandlightOn = newLightOnCommand(light); • remote.setCommand(lightOn); • remote.buttonWasPressed(); • } • } Erstellen der benötigten Objekte Zuweisung des Licht-An-Befehl
Aufgabenstellung • Implementiere den Befehl (Command) • Zeichne das dazugehörige Klassendiagramm Öffnen des Tores Es existiert eine Klasse „EntranceGate“
Auflösung publicclassOpenEntraceGateCommandextendsCommand { privateEntraceGategate; publicOpenEntraceGateCommand(EntraceGategate){ this.gate= gate; } publicvoidexecute() { gate.openGate(); } }
Rückgängig Funktion… Rückgängig Knopf
Erstellen des „ConreteCommand“ • Schnittstelle für Befehle • FuehreAus() – Fürt Funktion aus • Besitzt Befehl (Befehle) • Ruft Befehl auf • Weiß wie die Arbeit funktioniert • Kann jede Klasse sein • Verbinden Action und Receiver
Vielen Dank für eure Aufmerksamkeit Deimbacher, Gölles