Tuesday, 4 February 2014

Command Design pattern - Introduction

Master Core Java Architecture & Design Patterns!

Subscribe to Ram N Java for clean code principles, GoF design patterns, and step-by-step software engineering tutorials!

🔔 Subscribe to Ram N Java

What is the Command Design Pattern?

The Command Design Pattern is a foundational behavioral pattern established by the Gang of Four (GoF). Its primary intent is to encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

In conventional software architectures, user interfaces or calling components often invoke business operations directly. This tight coupling means that any change to the business layer requires rewriting UI triggers, and features like command queuing, scheduling, or history tracking become challenging to implement.

Core Concepts & Participants

The Command pattern solves this coupling by separating the object that invokes the command from the object that receives and executes it:

  • Command Interface: Declares an execution method (typically execute()) common to all concrete operations.
  • Concrete Command: Implements the Command interface, maintaining a reference to the receiver and delegating execution to the receiver's specific methods.
  • Invoker: Asks the command to carry out the request (e.g., a button, menu item, or remote control) without knowing which receiver handles it.
  • Receiver: Knows how to perform the actual business logic necessary to carry out the requested action.
  • Client: Creates and configures concrete command objects and binds them to the appropriate invokers.

Key Architectural Benefits

Introducing the Command pattern into your design offers notable software engineering advantages:

  • Decoupling Invoker and Receiver: Classes invoking operations are completely isolated from classes that execute them.
  • Single Responsibility Principle: Classes that trigger operations are kept separate from classes that implement domain logic.
  • Open/Closed Principle: New commands can be introduced seamlessly without altering existing client or invoker code.

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=iNKvqMiPtmY

Click the below Image to Enlarge
Command Design pattern - Introduction

Command Design pattern - Introduction


See also:

  • Command Design pattern - Real time example [Hotel]
  • Command Design pattern - Real time example [Menu]
  • Command Design pattern - Class Diagram
  • Command Design pattern - Sequence Diagram
  • Command Design pattern - Object Creation and flow
  • Command Design pattern - Implementation [Menu]
  • Command Design pattern - Key points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials