Showing posts with label Object Creation Flow. Show all posts
Showing posts with label Object Creation Flow. Show all posts

Wednesday, 5 February 2014

Command Design pattern - Object Creation and flow

Master Practical Java Architecture & Design Patterns!

Subscribe to Ram N Java for clean coding tutorials, GoF design patterns, and real-world system implementations!

🔔 Subscribe to Ram N Java

The Lifecycle & Object Creation in Command Pattern

Understanding the Command Design Pattern requires looking closely at who instantiates which object and how these objects interact across runtime boundaries. In traditional tightly coupled designs, caller objects directly create and trigger receiver dependencies, making it difficult to decouple the initiator from execution logic.

In the Command pattern, object creation is clearly segmented: the Client instantiates the receiver and passes it into the constructor of a concrete command object. The client then registers this configured command object with the Invoker.

Step-by-Step Execution Flow

The interaction flow across the participants follows a precise sequence:

  1. Initialization: The Client instantiates the Receiver (the business logic component).
  2. Command Binding: The Client creates the ConcreteCommand object, injecting the receiver reference into it.
  3. Invoker Configuration: The Client assigns the command object to the Invoker (e.g., UI button, scheduler, or queue).
  4. Triggering: When an event occurs, the Invoker calls command.execute() without any awareness of who the receiver is or what action will occur.
  5. Execution: Inside execute(), the ConcreteCommand forwards the execution call to the underlying receiver.action() method.

Key Architectural Benefits

Following this structured flow provides critical software engineering advantages:

  • Complete Separation of Concerns: The invoker has zero compile-time or runtime coupling to specific business receivers.
  • Configurable Runtime Behavior: Commands can be dynamically swapped or parameterized during application runtime without code changes.
  • Support for Asynchronous & Queued Execution: Because the request is a fully realized object, it can be scheduled, stored in a queue, or executed across thread boundaries.

Related Tutorials from Ram N Java


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

Click the below Image to Enlarge
Command Design pattern - Object Creation and flow


















See also:

  • Command Design pattern - Introduction
  • 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 - Implementation [Menu]
  • Command Design pattern - Key points
  • All Design Patterns Links
  • Tutorials