Luis Martin
Luis Martin

Reputation: 973

In what cases the Chain of Command design pattern is applicable?

Please somebody clears me up the mess in my head with these patterns:

I've seen sites in which both are the same (examples of Chain of Command which are the same as Chain of Responsibility), and other sites in which not.

This is what I understand about each:

CHAIN OF COMMAND:

A class let's call it CommandChain, with a property holding a list of "commands", which are instances of classes which implement the same interface. Say they all must implement onCommand(command, arguments).

CommandChain has the addCommand() method to register new commands within it, and runCommand() which accepts a command name, and its parameters. This method should loop through the list of commands until one of them responds, does the corresponding actions, and sends ok.

CHAIN OF RESPONSIBILITY

Just as I've seen in some sites, this would be pretty much the same, with this difference: instead of having a class storing a list of commands to loop through, each command instance would store a reference to the next command instance.

So, is this difference big enough to consider both design patterns different?

In what real cases are they applicable?

Upvotes: 2

Views: 5038

Answers (3)

Bill
Bill

Reputation: 95

The Chain of Responsibility is one of the Gang of Four's original designs, and you can find several examples implemented in PHP here:

http://www.php5dp.com/category/design-patterns/chain-of-responsibility/

One use of the pattern has been with "sniffer" programs for selecting the correct device for use with Web sites that have multiple configurations for different devices (phone, tablet, desktop). The CoR pattern avoids coupling between the client making the request and the handling object.

It is not hierarchal in that one "handling object" is not above another, but it is sequential in that it goes through a set of options until it finds the most appropriate for a given request.

The "Chain of Command" may be a misnomer for the CoR or it may be a wholly different pattern with a hierarchal structure.

Upvotes: 0

Anders Johansen
Anders Johansen

Reputation: 10455

They are the same design pattern.

If they have the same properties and only the implementation differs, they are functionally the same. This indicates that the two design patterns are one and the same.

Another hint is that there is a Wikipedia page for Chain of Responsibility DP, but none for Chain of Command.

The "Gang of Four" who wrote the seminal book on DPs were very clear on that behavior and properties, not implementation, defined a design pattern.

Upvotes: 6

Atul
Atul

Reputation: 1774

It looks like that Chain of Command is used when there is a single handler that needs to perform multiple task based on some request while on the other hand in Chain of Responsibility is used when each handler can handle only one type of request. An example could be if there is fire alarm, there you can issue a request to fire-detecter which will instead issue many commands to check the fire at different levels or places. While who will be respond to this fire like fire-warden, security guard, police etc can be done by using chain-of-responsibility.

Upvotes: 1

Related Questions