Reputation: 5441
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?
Upvotes: 4
Views: 3440
Reputation: 1
The CoR does not fit in your scenario because one premise of this pattern is breaking the chain once the request is handled by one of the handlers.
By your description, you are looking for something that works as a middleware, or in GoF terms, a decorator. Take a look in this pattern and compare it to the CoR, both can be confused sometimes.
Upvotes: 0
Reputation: 106351
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.:
Basically, this is one of those situations where I would advise against reinventing the wheel.....
Upvotes: 8
Reputation: 3686
Cannot agree more with mikera.
1) COR is not suited - Why? In addition to what mikera said, your problem definition talks about structure i.e You have sequence of operations (check credit, check inventory etc..) but you chose Chain of Responsibility, which is a behavioral pattern. I sense that something is not right.
2) Don't invent the wheel - especially if you are talking about financial instruments such as sales processing.
That said, on an entirely different note, you can use Facade design pattern to present a unified interface that encapsulates subsystems. Example here has a the same business case as your problem.
Upvotes: 0