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
Adapter pattern
PPTX
Command Design Pattern
PPT
Command pattern
PPT
Command Pattern
PPT
Flyweight pattern
PPT
Adapter Design Pattern
PPT
Command Design Pattern
PPTX
The Singleton Pattern Presentation
Adapter pattern
Command Design Pattern
Command pattern
Command Pattern
Flyweight pattern
Adapter Design Pattern
Command Design Pattern
The Singleton Pattern Presentation

What's hot (20)

PPT
Iterator Design Pattern
PPT
React js
PPTX
Visitor Pattern
PPSX
Observer design pattern
PPTX
React hooks
PPTX
Fly Weight Design Pattern.pptx
PPT
Bridge pattern
PDF
Important React Hooks
PDF
Java Course 11: Design Patterns
PDF
Behavioral Design Patterns
PDF
Introduction to Design Pattern
PPTX
Adapter Design Pattern
PPTX
REST & RESTful Web Services
PDF
Java Design Patterns Tutorial | Edureka
PPTX
Content provider in_android
PDF
Android Components
PPTX
Intro to React
PDF
PPTX
Introduction to Spring Framework
PPT
Mvc architecture
Iterator Design Pattern
React js
Visitor Pattern
Observer design pattern
React hooks
Fly Weight Design Pattern.pptx
Bridge pattern
Important React Hooks
Java Course 11: Design Patterns
Behavioral Design Patterns
Introduction to Design Pattern
Adapter Design Pattern
REST & RESTful Web Services
Java Design Patterns Tutorial | Edureka
Content provider in_android
Android Components
Intro to React
Introduction to Spring Framework
Mvc architecture
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
PDF
Mediator Design Pattern
PDF
Bridge Pattern
PPT
20120420 - Design pattern bridge
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
Mediator Design Pattern
Bridge Pattern
20120420 - Design pattern bridge
Ad

Similar to Command Design Pattern (20)

PDF
Command pattern in java
PDF
RMI (Remote Method Invocation)
PDF
Module 3 remote method invocation-2
PPTX
Remoting and serialization
PPTX
Remote Method Invocation (Java RMI)
PDF
Design patterns in Magento
PDF
PPT
Lesson10 behavioral patterns
PPT
Remote method invocation
PDF
Command pattern vs. MVC: Lean Beans (are made of this)
PDF
dotnet_remoting
PDF
Networked APIs with swift
PPT
Dot NET Remoting
DOCX
Memento Pattern Implementation
PPT
Tech talk
PDF
Distributed objects
PPTX
Java RMI
PDF
Remote Method Invocation in JAVA
PPTX
Remote method invocatiom
PPT
0903 1
Command pattern in java
RMI (Remote Method Invocation)
Module 3 remote method invocation-2
Remoting and serialization
Remote Method Invocation (Java RMI)
Design patterns in Magento
Lesson10 behavioral patterns
Remote method invocation
Command pattern vs. MVC: Lean Beans (are made of this)
dotnet_remoting
Networked APIs with swift
Dot NET Remoting
Memento Pattern Implementation
Tech talk
Distributed objects
Java RMI
Remote Method Invocation in JAVA
Remote method invocatiom
0903 1

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
MYSQL Presentation for SQL database connectivity
PPTX
A Presentation on Artificial Intelligence
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
A Presentation on Artificial Intelligence
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Spectroscopy.pptx food analysis technology
Building Integrated photovoltaic BIPV_UPV.pdf
Getting Started with Data Integration: FME Form 101
Programs and apps: productivity, graphics, security and other tools
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A comparative analysis of optical character recognition models for extracting...
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
“AI and Expert System Decision Support & Business Intelligence Systems”

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.