Skip to content

Command Design Pattern

Devrath edited this page May 24, 2021 · 3 revisions

Why we use this pattern

  • Assume we have designed a framework that has a button that has a custom-designed button. This framework is reused in several projects. In one project it is used to add a customer and in another project, it is used to add a product to a cart.
  • The button action should be independent and depend on how the clients that use it. meaning the client will determine the implementation of the click action.

Steps in setting up

  • Define the button class -> Have a method for click action.
  • Define the interface that has an abstract method that can be used to delegate the work from Button to the implementation that uses the framework
  • We need to define an implementation for the command by implementing the interface. In this class, we need to have a constructor that sets the reference of the implementation.
  • In the overridden method of the implementation, using the reference of the implementation, we need to call the function that contains the logic
  • Finally, define create the Implementation object -> then create the command object that takes the reference of implementation object in the constructor -> then pass the command to the button class -> the with button reference call the click

Projects
ViewWidgets

Project: ViewWidgets