This article explains Chain of Responsibility design pattern in java with UML class diagram. It then takes an example scenario in java and explains it with class diagram and code.
Introduction: Chain of Responsibility Design Pattern is a behavioral design pattern among the Gang Of Four(GOF)Article on GOF Patterns & their types Design Patterns. Being a behavioral design pattern, the Chain of Responsibility pattern deals with how objects of the designed system interact with each other.
What is Chain of Responsibility Design Pattern
Chain of Responsibility Pattern decouples the handler of a request from its sender by providing multiple potential handlers chained in a sequence. As the request reaches the first handler object, the object checks if it can handle this request. If yes, it handles the request, else it forwards the request to the next handler object in the chain.
Note that this process of handling or forwarding is recursive in nature. The recursive nature of the pattern reflects in its self-referencing design.
When to use Chain of Responsibility Design Pattern
- The handler of the request is to be determined dynamically: Which of the multiple handler objects will handle the request is not known in advance. It is determined at runtime.
- The handlers can be added dynamically: The potential handlers for the request can be added when the system is running. For example: In a hierarchical setup(like employee hierarchy in an organization) of handlers, the objects representing individual personnel at various levels in the hierarchy may not be available at system design time.
Class Diagram for Chain of Responsibility Design Pattern
Explanation of Chain of Responsibility Design Pattern’s Class Diagram
Handleris the base class for all handlers.
Handlerholds a self-referential association with itself to implement the recursive nature of handlers calling next handlers in chain.
Handlerand implement the actual logic of handling in the
Clientinitiates the request which is handled by the
ConcreteHandlerobjects in a sequential chain.
Chain of Responsibility Design Pattern’s Example Implementation in Java-Class Diagram
The Java class diagram above depicts Chain of Responsibility Design pattern implemented for a Student Complaint System. In this complaint system complaint request for students up to 4 past complaints is handled by the class teacher, between 5 to 9 past complaints is handled by the vice principal and more than 9 past complaints is handled by the principal.
Lets quickly go through whats there in Java’s example’s class diagram and corresponding code –
ComplaintRequestencapsulates the student complaint raised.
Staffis the base abstract class for all handlers in the chain. It holds a reference to itself which is named as successor in terms of next in the chain for handling the request. In this case successor is next
Staffobject greater in authority. Authority structure is ‘class teacher > vice principal > principal’.
ComplaintRequests are created an given to a
ClassTeacherinstance for handling. If
pastComplaintCountis <=4 then class teacher handles the complaint else it passes the complaint to its successor
VicePrincipalhandles complaints till
pastComplaintCountis <=9. Complaints with
pastComplaintCountgreater than 9 is given to the
Principalobject to handle.
- The output is shown for 3 complaint requests raised which are handled by class teacher, vice principal and principal.
In the above tutorial we understood what is Chain of Responsibility design pattern and the main scenarios in which this pattern is applicable. We then looked at the UML class diagram for Chain of Responsibility Design Pattern & its explanation, a Java Use Case implementing the pattern with its class diagram and code for the classes shown in the class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on Chain of Responsibility design pattern.