Design pattern > Comportementaux > Command
Ce patron emboîte une demande dans un objet, permettant de paramétrer, mettre en file d'attente, journaliser et annuler des demandes.
Dans ce patron un objet command correspond à une opération à effectuer.
L'interface de cet objet comporte une méthode execute.
Pour chaque opération, l'application va créer un objet différent qui implémente cette interface - qui comporte une méthode execute.
L'opération est lancée lorsque la méthode execute est utilisée. Ce patron est notamment utilisé pour les barres d'outils
Command (Command)
- declares an interface for executing an operation
ConcreteCommand (CalculatorCommand)
- defines a binding between a Receiver object and an action
- implements Execute by invoking the corresponding operation(s) on Receiver
Client (CommandApp)
- creates a ConcreteCommand object and sets its receiver
Invoker (User)
- asks the command to carry out the request
Receiver (Calculator)
- knows how to perform the operations associated with carrying out the request.
Current value = 100 (following + 100)
Current value = 50 (following - 50)
Current value = 500 (following * 10)
Current value = 250 (following / 2)
---- Undo 4 levels
Current value = 500 (following * 2)
Current value = 50 (following / 10)
Current value = 100 (following + 50)
Current value = 0 (following - 100)
---- Redo 3 levels
Current value = 100 (following + 100)
Current value = 50 (following - 50)
Current value = 500 (following * 10)