SlideShare a Scribd company logo
3
Most read
4
Most read
18
Most read
Command Pattern When Actions Speak Louder Than Words Shahriar Hyder Kaz Software Ltd. 17 th  October, 2011
Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations
Command Pattern An object is used to represent and encapsulate all the information needed to call a method at a later time This information includes the method name, the object that owns the method, and the values for the method parameters Three fundamental Command pattern terms: Client : instantiates command object and provides information to call the method at a later time Invoker : decides which method should be called (Executes the Commands Possibly at a later time) Receiver :   an instance of the class that contains the method's code (The object the command should affect)
Class Diagram Client Invoker setCommand() <<interface>> Command execute() undo() Receiver action() ConcreteCommand execute() undo() public void execute() { receiver.action() }
Collaborations The  Client   creates a  ConcreteCommand   object and specifies its  receiver . An  Invoker   object stores the  ConcreteCommand   object. The  invoker   issues a request by calling  execute ( ) on the command.  The  ConcreteCommand  object  invokes  operations on its  receiver  to carry out the request.
Sequence Diagram aReceiver anInvoker aCommand aClient new Command (Rx) StoreCommand(Command) Execute( ) Action( )
 
 
 
 
 
 
 
 
 
More uses Queuing requests Can allocate commands to various threads for processing to load balance between threads/processors Logging requests (audit trail) Just have to save the command objects as they execute. If something goes wrong, we can read the log and re-create the sequence of commands (so no data is lost). Can also back out changes that cause troubles Command Pattern can model transaction systems
Main Concepts It decouples an object making a request, from the one that knows how to perform it the command requester only needs to know how to issue it; it doesn't need to know how to perform it Command object is at the center of this decoupling and encapsulates a receiver with an action An invoker makes a request of a Command object by calling its execute() method, which invokes those actions on the receiver Invokers can be parameterized with Commands, even at runtime Macro Commands: an extension of Command that allow multiple commands (Composite) to be invoked Two user interfaces may share an instance of the same concrete Command subclass. Commands are first-class objects.  They can be manipulated and extended like any other object. Two important aspects of the Command pattern:  1. interface separation (the invoker is isolated from the receiver),  2. time separation (stores a ready-to-go processing request that’s to be stated later).
Related Patterns! Chain of Responsibility can use Command to represent requests as objects. Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value. Memento can hold the state a command requires to undo its effect! Composite can be used to implement macro commands! Commands that are copied before being placed on a history list act as Prototypes!
 

More Related Content

PPT
Command Pattern
PPT
Command Design Pattern
PPTX
Command Design Pattern
PPTX
Design Pattern - Factory Method Pattern
PPTX
The Singleton Pattern Presentation
PDF
Introduction to Design Pattern
PPTX
Adapter Design Pattern
PDF
Design Patterns Presentation - Chetan Gole
Command Pattern
Command Design Pattern
Command Design Pattern
Design Pattern - Factory Method Pattern
The Singleton Pattern Presentation
Introduction to Design Pattern
Adapter Design Pattern
Design Patterns Presentation - Chetan Gole

What's hot (20)

PPT
Command pattern
PPT
Flyweight pattern
PPTX
Design pattern-presentation
PPTX
Let us understand design pattern
PPT
Builder pattern
PPT
Design patterns ppt
PPTX
Design pattern (Abstract Factory & Singleton)
PPT
Chain of responsibility
PPTX
Factory Method Pattern
PPTX
Presentation on Template Method Design Pattern
PPTX
Factory Design Pattern
PPTX
Creational pattern
PPT
Iterator Design Pattern
PPT
Prototype pattern
PPTX
Java Beans
PPSX
Observer design pattern
PPT
Adapter pattern
PPTX
Gof design patterns
PPTX
PPTX
Builder pattern
Command pattern
Flyweight pattern
Design pattern-presentation
Let us understand design pattern
Builder pattern
Design patterns ppt
Design pattern (Abstract Factory & Singleton)
Chain of responsibility
Factory Method Pattern
Presentation on Template Method Design Pattern
Factory Design Pattern
Creational pattern
Iterator Design Pattern
Prototype pattern
Java Beans
Observer design pattern
Adapter pattern
Gof design patterns
Builder pattern
Ad

Viewers also liked (20)

PDF
Command Pattern in Ruby
PPTX
Design Pattern lecture 4
PDF
Design patterns - Singleton&Command
PPT
Command and Adapter Pattern
PPTX
Chain of Responsibility Pattern
PPT
Generalized Functors - Realizing Command Design Pattern in C++
PPT
Composite Design Pattern
PPTX
Bridge Design Pattern
PPTX
Command Pattern
PDF
An Introduction to
PPTX
Command Pattern
PDF
Client-Server-Kommunikation mit dem Command Pattern
PPTX
Model View Command Pattern
PPT
Design Patterns
PPTX
Proxy Pattern
PPTX
Chain of Responsibility Pattern
PPS
Bridge Pattern Derek Weeks
PPT
Bridge pattern
PDF
Mediator Design Pattern
PDF
Bridge Pattern
Command Pattern in Ruby
Design Pattern lecture 4
Design patterns - Singleton&Command
Command and Adapter Pattern
Chain of Responsibility Pattern
Generalized Functors - Realizing Command Design Pattern in C++
Composite Design Pattern
Bridge Design Pattern
Command Pattern
An Introduction to
Command Pattern
Client-Server-Kommunikation mit dem Command Pattern
Model View Command Pattern
Design Patterns
Proxy Pattern
Chain of Responsibility Pattern
Bridge Pattern Derek Weeks
Bridge pattern
Mediator Design Pattern
Bridge Pattern
Ad

Similar to Command Design Pattern (20)

PDF
Command pattern in java
PPTX
How do i implement command design pattern in the java programming course with...
PPTX
How do i implement command design pattern in the java programming course with...
PPT
Command presentation on design pattern.
PPT
Commanda gfin it show the hole idea of command
PPT
Lesson10 behavioral patterns
PPTX
Behavioral pattern 4
PDF
Command pattern vs. MVC: Lean Beans (are made of this)
PPTX
2. Design patterns. part #2
PPTX
Go f designpatterns 130116024923-phpapp02
PDF
A coding fool design patterns
PPT
Command Pattern Geoff Burns 2006 Nov
PPTX
Evolution of Patterns
PPT
Design Patterns
PDF
Command & [e]Mission Control: Using Command and Event Buses to create a CQRS-...
PPT
Design Pattern with Actionscript
PPT
Ch08lect3 ud
PPTX
PATTERNS06 - The .NET Event Model
PDF
UML Design Class Diagrams (2014)
PPT
ActionScript Design Patterns
Command pattern in java
How do i implement command design pattern in the java programming course with...
How do i implement command design pattern in the java programming course with...
Command presentation on design pattern.
Commanda gfin it show the hole idea of command
Lesson10 behavioral patterns
Behavioral pattern 4
Command pattern vs. MVC: Lean Beans (are made of this)
2. Design patterns. part #2
Go f designpatterns 130116024923-phpapp02
A coding fool design patterns
Command Pattern Geoff Burns 2006 Nov
Evolution of Patterns
Design Patterns
Command & [e]Mission Control: Using Command and Event Buses to create a CQRS-...
Design Pattern with Actionscript
Ch08lect3 ud
PATTERNS06 - The .NET Event Model
UML Design Class Diagrams (2014)
ActionScript Design Patterns

More from Shahriar Hyder (8)

PPTX
Effective Communication Skills for Software Engineers
DOCX
A JavaScript Master Class - From the Wows to the WTFs
PPTX
Dependency Inversion Principle
PPTX
Taking a Quantum Leap with Html 5 WebSocket
PPTX
Functional Programming Fundamentals
PPT
Object Relational Mapping with LINQ To SQL
PPT
C# 3.0 Language Innovations
PPT
Introduction to Linq
Effective Communication Skills for Software Engineers
A JavaScript Master Class - From the Wows to the WTFs
Dependency Inversion Principle
Taking a Quantum Leap with Html 5 WebSocket
Functional Programming Fundamentals
Object Relational Mapping with LINQ To SQL
C# 3.0 Language Innovations
Introduction to Linq

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Spectroscopy.pptx food analysis technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25 Week I
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
Spectroscopy.pptx food analysis technology
MIND Revenue Release Quarter 2 2025 Press Release
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Command Design Pattern

  • 1. Command Pattern When Actions Speak Louder Than Words Shahriar Hyder Kaz Software Ltd. 17 th October, 2011
  • 2. Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations
  • 3. Command Pattern An object is used to represent and encapsulate all the information needed to call a method at a later time This information includes the method name, the object that owns the method, and the values for the method parameters Three fundamental Command pattern terms: Client : instantiates command object and provides information to call the method at a later time Invoker : decides which method should be called (Executes the Commands Possibly at a later time) Receiver : an instance of the class that contains the method's code (The object the command should affect)
  • 4. Class Diagram Client Invoker setCommand() <<interface>> Command execute() undo() Receiver action() ConcreteCommand execute() undo() public void execute() { receiver.action() }
  • 5. Collaborations The Client creates a ConcreteCommand object and specifies its receiver . An Invoker object stores the ConcreteCommand object. The invoker issues a request by calling execute ( ) on the command. The ConcreteCommand object invokes operations on its receiver to carry out the request.
  • 6. Sequence Diagram aReceiver anInvoker aCommand aClient new Command (Rx) StoreCommand(Command) Execute( ) Action( )
  • 7.  
  • 8.  
  • 9.  
  • 10.  
  • 11.  
  • 12.  
  • 13.  
  • 14.  
  • 15.  
  • 16. More uses Queuing requests Can allocate commands to various threads for processing to load balance between threads/processors Logging requests (audit trail) Just have to save the command objects as they execute. If something goes wrong, we can read the log and re-create the sequence of commands (so no data is lost). Can also back out changes that cause troubles Command Pattern can model transaction systems
  • 17. Main Concepts It decouples an object making a request, from the one that knows how to perform it the command requester only needs to know how to issue it; it doesn't need to know how to perform it Command object is at the center of this decoupling and encapsulates a receiver with an action An invoker makes a request of a Command object by calling its execute() method, which invokes those actions on the receiver Invokers can be parameterized with Commands, even at runtime Macro Commands: an extension of Command that allow multiple commands (Composite) to be invoked Two user interfaces may share an instance of the same concrete Command subclass. Commands are first-class objects. They can be manipulated and extended like any other object. Two important aspects of the Command pattern: 1. interface separation (the invoker is isolated from the receiver), 2. time separation (stores a ready-to-go processing request that’s to be stated later).
  • 18. Related Patterns! Chain of Responsibility can use Command to represent requests as objects. Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value. Memento can hold the state a command requires to undo its effect! Composite can be used to implement macro commands! Commands that are copied before being placed on a history list act as Prototypes!
  • 19.