skip to Main Content

Welcome

Say hello to the toggle bar. This is an optional section you can use to display any content you'd like. Simply select a page from the theme panel and the content of the page will display here. You can even use the drag and drop builder to create this! This is a perfect place for your company mission statement, alerts, notices or anything else.

Get In Touch

Email: support@total.com
Phone: 1-800-Total-Theme
Address: Las Vegas, Nevada

Our Location

togglebar-map

Command Pattern

Post Series: Behavioral Design Pattern

In this pattern, a request is wrapped under an object as a command and passed to invoker object. Invoker object pass the command to the appropriate object which can handle it and that object executes the command. This handles the request in traditional ways like as queuing and callbacks.

This pattern is commonly used in the menu systems of many applications such as Editor, IDE etc.

  • Client: This is the class that creates and executes the command object.
  • Invoker: Asks the command to carry out the action.
  • Command: This is an interface which specifies the Execute operation (optinal parameter).
  • ConcreteCommand: This is a class that implements the Execute operation by invoking operation(s) on the Receiver.
  • Receiver: This is a class that performs the Action associated with the request. (Responsitory, Request)

Implement

Note

InsertCommand in ADO.NET is an example

Advantages and disadvantages

Advantages

  • It decouples the classes that invoke the operation from the object that knows how to execute the operation
  • It allows you to create a sequence of commands by providing a queue system
  • Extensions to add a new command is easy and can be done without changing the existing code
  • You can also define a rollback system with the Command pattern, for example, in the Wizard example, we could write a rollback method

Disadvantages

  • There are a high number of classes and objects working together to achieve a goal. Application developers need to be careful developing these classes correctly.
  • Every individual command is a ConcreteCommand class that increases the volume of classes for implementation and maintenance.

When to use it?

  1. Need to implement callback functionalities.
  2. Need to support Redo and Undo functionality for commands.
  3. Sending requests to different receivers which can handle it in different ways.
  4. Need for auditing and logging of all changes via commands.

Resource

http://www.dotnettricks.com/learn/designpatterns/command-design-pattern-dotnet

https://quachson.wordpress.com/2011/04/22/design-pattern-command/

https://www.packtpub.com/mapt/book/application_development/9781785888038/7/ch07lvl1sec56/advantages-and-disadvantages-of-command-patterns

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top