Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain of Responsibility [GoF] disadvantages

We need to build a solution to process sales orders. The processing is done serially: each step takes care of specific tasks: check if the client has credit, check if the required item is in stock, etc.

We thought of using the chain of responsibility pattern.

I found this old but very valuable article. It starts by comparing CoR with the Template pattern. Since we are not concerned about coupling, both of them seem to work.

Are there any disadvantages (or pitfalls, etc) that I should be aware?

like image 615
Bob Rivers Avatar asked Feb 20 '23 08:02

Bob Rivers


1 Answers

It's not really a good fit: the Chain of Responsibility pattern is about delegating tasks that the earlier steps can't handle.

In your case you want every step to be applied so CoR doesn't really apply.

What you really want here is some kind of business process engine like jBPM. Process engines are designed to handle exactly this kind of situation and give you many features that would be tricky to implement in a hand-rolled solution, e.g.:

  • The ability to have long-running processes that persist to a database. It's not amusing trying to debug order that get lost because you had to reboot a server....
  • Ability to wait for external events on message queues (e.g. getting a confirmation from a remote system). Might not need this in some simple cases but it becomes essential if you are integrating with external suppliers.
  • Handling for exceptional conditions and process rollback/compensation
  • Event logging and reporting

Basically, this is one of those situations where I would advise against reinventing the wheel.....

like image 149
mikera Avatar answered Mar 01 '23 07:03

mikera