Chain of Responsibility Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) chain of responsibility design pattern. By reading this tutorial, you will know how to develop a model for the chain of responsibility pattern, and how to apply it in practice.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Chain of Responsibility.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it Client.

- Move the mouse cursor over the Client class and drag out Association > Class to create an associated class named Handler.

- Right-click on Handler and select Model Element Properties > Abstract to set it as abstract.

- Right-click on the Handler class and select Add > Operation from the popup menu.

- Name the operation HandleRequest().
- Right-click on HandleRequest and select Model Element Properties > Abstract to set it as abstract.

- Move the mouse cursor over the Handler class and click on the Self Association resource icon to create a self-association.

- Name the association end "successor."

- Move the mouse cursor over the Handler class and drag out Generalization > Class to create a subclass named ConcreteHandler.

- In practice, there may be multiple concrete handlers. To represent this, stereotype the ConcreteHandler class as PTN Cloneable. Right-click on ConcreteHandler and select Stereotypes > Stereotypes... from the popup menu.

- In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to the ConcreteHandler class. Click OK to confirm.

-
You need to make the concrete handlers inherit operations from the handler class. Right-click on ConcreteHandler and select Related Elements > Realize all Interfaces from the popup menu.
Up to now, the diagram should look like this:
Defining a Pattern
- Select all classes on the class diagram.

- Right-click on the selection and select Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Chain of Responsibility. Keep the file name as is and click OK to proceed.

Applying a Design Pattern to a Class Diagram
In this section, we will apply the chain of responsibility pattern to model a coin dispenser.
- Create a new project named Coin Dispenser.
- Create a class diagram named Domain Model.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Chain of Responsibility from the list of patterns.

- Click on Handler in the overview.

- Rename Handler to CoinDispenser and the HandleRequest operation to Dispense in the bottom pane.

- Click on ConcreteHandler in the overview and rename it to DollarDispenser and the HandleRequest operation to Dispense.

- You need one more concrete handler for dispensing cents. Keep ConcreteHandler selected, click on + and select Clone... from the popup menu.

- Enter 1 as the number of classes to clone and click OK to confirm.

- Rename ConcreteHandler2 to CentDispenser and the HandleRequest operation to Dispense.

- Click OK to apply the pattern to the diagram.
- Tidy up the diagram. The result should look like this:
