Questions tagged [command-pattern]

The Command pattern is a Gang of Four behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

The Command pattern is a Gang of Four behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time.

The object used includes the method name, the object that owns the method and values for the method parameters.

Typical uses of the Command pattern are, among others:

  • Undo
  • Transactional behavior
  • Wizards
  • ThreadPools

This is one of the Gang of Four's behavioral , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

More information is available on Wikipedia.

303 questions
121
votes
7 answers

Using a strategy pattern and a command pattern

Both design patterns encapsulate an algorithm and decouple implementation details from their calling classes. The only difference I can discern is that the Strategy pattern takes in parameters for execution, while the Command pattern doesn't. It…
Extrakun
  • 18,259
  • 19
  • 74
  • 122
101
votes
15 answers

Long list of if statements in Java

Sorry I can't find a question answering this, I'm almost certain someone else has raised it before. My problem is that I'm writing some system libraries to run embedded devices. I have commands which can be sent to these devices over radio…
Steve
  • 19,541
  • 21
  • 63
  • 90
92
votes
4 answers

Well designed query commands and/or specifications

I've been searching for quite some time for a good solution to the problems presented by the typical Repository pattern (growing list of methods for specialized queries, etc.. see: http://ayende.com/blog/3955/repository-is-the-new-singleton). I…
Erik Funkenbusch
  • 90,480
  • 27
  • 178
  • 274
84
votes
4 answers

CQRS: Command Return Values

There seems to be endless confusion about whether commands should or should not have return values. I would like to know if the confusion is simply because the participants have not stated their context or circumstances. The Confusion Here are…
Brent Arias
  • 26,187
  • 32
  • 120
  • 209
83
votes
6 answers

Difference between Strategy pattern and Command pattern

What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java.
Krishna
  • 6,706
  • 15
  • 64
  • 79
64
votes
13 answers

Command Pattern : How to pass parameters to a command?

My question is related to the command pattern, where we have the following abstraction (C# code) : public interface ICommand { void Execute(); } Let's take a simple concrete command, which aims to delete an entity from our application. A Person…
Romain Verdier
  • 12,297
  • 7
  • 53
  • 77
50
votes
2 answers

What is the difference between Command + CommandHandler and Service?

I have been reading about using Command objects to represent use cases that our domain exposes, and Command Handler objects to process those commands. For example: RegisterUserCommand RegisterUserCommandHandler But it looks exactly the same as…
Matthieu Napoli
  • 42,736
  • 37
  • 154
  • 239
45
votes
8 answers

Why should I use the command design pattern while I can easily call required methods?

I am studying the command design pattern, and I am quite confused with the way of using it. The example that I have is related to a remote control class that is used to turn lights on and off. Why should I not use the switchOn() / switchOff()…
Daniel Newtown
  • 2,693
  • 8
  • 25
  • 60
40
votes
9 answers

command pattern returning status

Once I had a discussion about design, relative to the command pattern. My peer stated that a command object should not return the status (successful, unsuccessful, and why) after the .execute() method is called. The reason is that you should not be…
Stefano Borini
  • 125,999
  • 87
  • 277
  • 404
35
votes
6 answers

Callback/Command vs EventListener/Observer Pattern

I'm trying to design an async framework and wanted to know what people think are the pros/cons of the callback pattern vs the observer pattern. Callback pattern: //example callback public interface Callback{ public void notify(MethodResult…
DD.
  • 19,793
  • 49
  • 140
  • 237
25
votes
2 answers

What is the meaning of dashed arrow in UML class diagram?

What is the meaning of the dashed arrow in a UML class diagram in general? As an example: What is the meaning of the dashed arrow in this image (from client to ConcreteCommand): Article: Command pattern
Martin Thoma
  • 91,837
  • 114
  • 489
  • 768
17
votes
8 answers

Why is the Command Pattern convenient in Object-Oriented Design?

I don't understand why a Command pattern is convenient in object-oriented design. Instead of using, e.g. the Command Switch which has a reference to the Lamp class, can't I just create a Switchable abstract class and invoke its methods? In this way…
aneuryzm
  • 55,858
  • 96
  • 259
  • 471
16
votes
4 answers

Command Pattern seems needlessly complex (what am I failing to understand?)

I've read up on the Command Pattern, and I think I'm missing something. The Command object exists to abstract away the details of the Receiver object. It seems to me that we could simply stop here, and hold references to Command objects to execute…
16
votes
5 answers

How do I implement a simple undo/redo for actions in java?

I've created an XML editor and I'm stuck at the last phase: adding undo/redo functionality. I've only got to add undo/redo for when users add elements, attributes, or text to the JTree. I'm still quite new at this but in school today I attempted…
Chea Indian
  • 597
  • 3
  • 8
  • 16
15
votes
6 answers

Using Command Design pattern

Can anyone explain with a simple example the Command Pattern? I tried searching on the internet, but I got confused.
RKCY
  • 3,757
  • 11
  • 53
  • 93
1
2 3
20 21