A Delicious Example

In a given restaurant, there is probably a hierarchy in which the people who work at that establishment are placed. When a kitchen needs supplies, the Head Chef of that kitchen is likely to be the first one to notice. So, s/he might need to file a purchase request with his/her bosses in order to obtain new equipment, such as knives or cutting boards or even larger items like ovens.

In our kitchen specifically, the purchase request system operates like this:

The Head Chef has implicit approval to purchase any item which is less than $1000 USD.

If the total amount of the purchase is greater than that but less than $2500, the Head Chef must get the restaurant's Purchasing Manager's approval for the purchase.

If the total amount of the purchase is greater than $2500 but less than $10000, then the head chef must get the approval of the restaurant's General Manager to make the purchase.

Finally, if the purchase amount is greater than $10000, the General Manager will call an executive meeting to determine if they need to make the purchase requested.

There's a hierarchy in play here: Head Chef > Purchasing Manager > General Manager. We can model this purchasing system using the Chain of Responsibility design pattern.

Firstly, let's model the object that represents the purchase order itself.

Notice that each person in the hierarchy (e.g. each link in the chain) can call its own supervisor to make a determination as to whether or not the item can be purchased. This is part of the Chain of Responsibility design pattern: each link is aware of its own successor.

Finally, we need a Client participant, which in this case is our Main().

Notice that all requests initially flow to Jennifer, the head chef, but if the request's total price is greater than certain amounts then the requets automatically flow to Mitchell the purchasing manager or Olivia the general manager.

The output of this sample project looks like this:

Each request flows through the chain until a link handles it. That's the definition of the Chain of Responsibility Design Pattern!

Will I Ever Use This Pattern?

Rarely. I personally haven't used it at all, but I can see why it would be useful in situations where there's a hierarchy of objects and each one could handle a particular request. But, as always, I'd love to hear about real-world uses of this pattern, so feel free to share in the comments!

Summary

The Chain of Responsibility pattern allows for multiple objects in a chain to make a pass at handling a request object. The request flows through the chain until a link in the chain handles it.

As always, I like to provide code with my tutorials, so the repository for this pattern is over on GitHub and contains all of the sample code used here.

Now I need to go have a talk with Olivia. She never approves my requests. Just because I burnt all the sausages last week doesn't mean I don't need that automatic mini donut factory I asked for! I mean, look at it! It's beautiful!