SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 28 / 42

Middle

What is the Command pattern? Where do you meet it in a Java backend?

Short answer

Command turns a request into an object (execute(), sometimes undo()), separating who creates the command from who runs it. Once a command is an object, you can queue, log, retry, run it later, or undo it.

execute()failureClient creates commandQueue / command logInvoker: executor, workerReceiver does the workRetry or undo

3 points to say:

  1. Command = a request packaged as an object: who creates it, who runs it and when are decoupled.
  2. In Java: Runnable/Callable submitted to an executor, job queues, undo stacks, command messages via a broker/outbox.
  3. Versus Strategy: a Command is "what to do" (one action with its data), a Strategy is "how to do it".

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.