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
14
votes
1 answer

Dependency Injection when using the Command Pattern

I'm using the Command Pattern for the first time. I'm a little unsure how I should handle dependencies. In the code below, we dispatch a CreateProductCommand which is then queued to be executed at a later time. The command encapsulates all the…
Ben Foster
  • 32,767
  • 35
  • 157
  • 274
14
votes
2 answers

Implementing the command pattern

I am in the design process of an application, and I would like to use the command pattern for undo/redo purposes. I did some research into the command pattern but the only thing I don't get is: Should a command have the undo and redo methods, or…
slayerIQ
  • 1,478
  • 1
  • 13
  • 25
13
votes
1 answer

Offline sync and event sourcing

The CRUD-based part of our application needs: Offline bidirectional "two-way" syncing Ability to modify data until ready and then "publish". Audit log Event Sourcing (or the "command pattern") is what I'm looking at to accomplish these items. I…
Joel
  • 2,333
  • 3
  • 27
  • 41
13
votes
4 answers

Real world example of application of the command pattern

Command pattern can be used to implement Transactional behavior (and Undo). But I could not find an example of these by googling. I could only find some trivial examples of a lamp that is switched on or off. Where can I find a coding example…
Jim
  • 17,102
  • 31
  • 115
  • 227
12
votes
2 answers

Get result of executed method in Command Pattern

Currently I'm trying to implement Transaction Script pattern (Exactly how Martin Fowler described by using Command Pattern) in a simple test project, everything just work fine, the problem is where I don't know how to get result(s) when specified…
saber
  • 5,999
  • 10
  • 49
  • 83
10
votes
3 answers

What is the Action Design Pattern?

What is the Action Design Pattern, I haven't heard of it before? I am suspecting it is the same as the Command Design pattern [wikipedia] but I can't find any resources on it.
hhafez
  • 36,343
  • 33
  • 109
  • 143
10
votes
3 answers

How to share the same context with different threads in multi-Command Pattern in C#?

There is an extended implementation of command pattern to support multi-commands (groups) in C#: var ctx= //the context object I am sharing... var commandGroup1 = new MultiItemCommand(ctx, new List { new Command1(ctx), …
Giannis Grivas
  • 3,187
  • 1
  • 13
  • 36
10
votes
2 answers

Command Design Pattern - Is Invoker Optional?

Is Invoker class optional in Command design pattern? Client needs to instantiate Concrete Command and Receiver for the command. Does client always need to instantiate Invoker and pass on the command object to Invoker object. Later on whenever client…
jags
  • 1,962
  • 24
  • 32
9
votes
7 answers

How to share the same context between commands in Command-Pattern with C#?

I've implemented the command pattern (in a multi-support way) in my application. Structure: class MultiCommand : BaseCommand abstract class BaseCommand : ICommand Process Flow: var commandsGroup = new MultiCommand(new List() …
Giannis Grivas
  • 3,187
  • 1
  • 13
  • 36
9
votes
1 answer

Why use the command pattern in GWT (or any web app)?

According to this video here [@ 7:50] Google is recommending the use of the Command pattern on top of its request handling API. There is also a helpful looking project gwt-dispatch that implements that pattern. According to gwt-dispatch…
HDave
  • 20,105
  • 26
  • 133
  • 216
8
votes
3 answers

using the command and factory design patterns for executing queued jobs

I have a list of jobs queued in the database which I need to read from database and execute them in parallel using threading and I have a list of command classes to execute each of those jobs all implementing a common interface (command pattern).…
RKP
  • 4,975
  • 20
  • 65
  • 109
8
votes
2 answers

How to get data back from a command bus?

I'm fairly new to domain driven design concepts and I've run into a problem with returning proper responses in an API while using a command bus with commands and command handlers for the domain logic. Let's say we’re building an application with a…
8
votes
2 answers

Using Command pattern for undo and redo in ArrayLists

So I have a program where you can log in and add/remove friends to and from the friends arraylist. Also I can like a certain thing and that thing will be stored into the likes arraylist. I'm asked to make undo and redo options for whichever action I…
Ultimania
  • 105
  • 1
  • 2
  • 6
8
votes
4 answers

Command Pattern vs. Visitor Pattern

Is it generally acceptable to allow a Visitor to modify state of the Receiver, or should that be a Command pattern instead?
grefly
  • 1,151
  • 2
  • 11
  • 28
8
votes
2 answers

Command pattern - why encapsulate in an object?

Command pattern is for encapsulating commands in objects. But why not use function pointers instead? Why do I need to subclass Command for each operation? Instead I can have different functions and call the function pointers.
Narek
  • 35,407
  • 69
  • 202
  • 359
1
2
3
20 21