Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a "receiver" class in the Command design pattern

I am learning command design pattern. As far as I know, four terms always associated with the command pattern are command, receiver, invoker and client.

A concrete command class has an execute() method and the invoker has a couple of commands. The invoker decides when to call the execute() method of a command.

When the execute() method is called, it calls a method of the receiver. Then, the receiver does the work.

I don't understand why do we need the receiver class? We can do the work inside execute() method, it seems that the receiver class is redundant.

Thank in advance.

like image 507
Kenny Lee Avatar asked May 15 '13 09:05

Kenny Lee


People also ask

What is a receiver in Command pattern?

A Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. (A sender is an object that invokes an operation, and a receiver is an object that receives the request to execute a certain operation.

Why do we need command design patterns?

The command pattern should be used when: You need a command to have a life span independent of the original request, or if you want to queue, specify and execute requests at different times. You need undo/redo operations. The command's execution can be stored for reversing its effects.

How does the Command design pattern work?

The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parametrized with different requests. The "check" at a diner is an example of a Command pattern. The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check.

What is the intent of Command design pattern?

Intent. Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request's execution, and support undoable operations.


2 Answers

Design patterns are used to solve software problems.

You have to understand the problem before trying to understand the solution (in this case Command pattern)

The problems which command pattern apply are in the context of an object A (client) invoking a method in an object B (receiver), so the Receiver is part of the problem, not part of the solution.

The solution or idea that command pattern offers is to encapsulate the method invocation from A to B in an object (Command), in fact this is close to the formal pattern definition. When you manage a request as an object you are able to solve some problems or to implement some features. (you also will need other pieces like the one called Invoker)

This list can give you some good examples of what kind of problems o features are suitable for command pattern.

note: Comamnd pattern is not necesary about decoupling, in fact the most common example pattern immplementation, the client needs to make a new instance of the receiver so we cannot talk about decoupling here.

like image 175
Gabriel Aramburu Avatar answered Mar 18 '23 15:03

Gabriel Aramburu


Imagine a class that can do couple of things, like Duck, it can eat and quack. Duck is a receiver in this example. To apply command pattern here, you need to be able to wrap eating and quacking into a command. They should be separate classes that derive from Command base class with execute() method because Duck can have only single execute() method. So EatCommand.execute() calls Duck.eat() and QuackCommand.execute() calls Duck.quack().

like image 34
Andriy Tylychko Avatar answered Mar 18 '23 14:03

Andriy Tylychko