GofPatterns
GofPatterns
SiteMap
Course
Behavior
Creation
Structure
Behavioral Patterns
«Prev
Next»
Chain of Responsibility
Chain Responsibility Code
Command Pattern
Command Pattern Code
Interpreter Pattern
Interpreter Pattern Code
Iterator Pattern
Iterator Pattern Code
Mediator Pattern
Mediator Pattern Code
Memento Pattern
Observer Pattern
Observer Pattern Code
State Pattern
State Pattern Code
Strategy Pattern
Template Pattern
Visitor Pattern
Externalize Stack
Hierarchical Visitor
Command Pattern
The Command pattern encapsulates a request in an object, which enables you to store the command, pass the command to a method, and return the command like any other object.
The figure below illustrates the Command Pattern.
Invoker aggregates an abstract Command.
Command Pattern in Java
Benefits:
The following lists the benefits of using the Command Pattern:
Separates the object that invokes the operation from the one that knows how to perform it.
It's easy to add new commands, because you don't have to change existing classes.
When to Use:
You should use the Command pattern when:
You want to parameterize objects by an action to perform.
You specify, queue, and execute requests at different times.
You must support undo, logging, or transactions