Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Chain of Responsibility - Python Design Patterns
Next article icon

Chain of Responsibility Design Pattern in Java

Last Updated : 10 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Chain of Responsibility Design Pattern in Java is a behavioral design pattern that allows an object to pass a request along a chain of handlers. Each handler in the chain decides either to process the request or to pass it along the chain to the next handler.

Chain-of-Responsibility-Design-Pattern-in-Java-

Important Topics for Chain of Responsibility Design Pattern in Java

  • What is the Chain of Responsibility Design Pattern in Java?
  • Characteristics of the Chain of Responsibility Design Pattern in Java
  • Real-World Analogy of the Chain Of Responsibility Design Pattern
  • Components of the Chain of Responsibility Design Pattern
  • Chain of Responsibility Design Pattern Example
  • Advantages of the Chain of Responsibility Design Pattern
  • Disadvantages of the Chain of Responsibility Design Pattern

What is the Chain of Responsibility Design Pattern in Java?

Chain of Responsibility Pattern or Chain of Responsibility Method in Java is a Behavioral Design Pattern, which allows an object to send a request to other objects without knowing who is going to handle it.

  • This pattern is frequently used in the chain of multiple objects, where each object either handles the request or passes it on to the next object in the chain if it is unable to handle that request.
  • This pattern encourages loose coupling between sender and receiver, providing freedom in handling the request.

Characteristics of the Chain of Responsibility Design Pattern in Java

Below are some characteristic of chain of responsibility design pattern in Java:

  • Loose Coupling: The pattern promotes loose coupling between the sender and receiver of a request, as the sender doesn't need to know which object will handle the request and the receiver doesn't need to know the structure of the chain. 
  • Dynamic Chain: The chain can be modified dynamically at runtime, allowing for flexibility in adding or removing handlers without affecting the client code.
  • Single Responsibility Principle: Each handler in the chain has a single responsibility, either handling the request or passing it to the next handler, which helps in maintaining a clean and modular design.
  • Sequential Order: Requests are processed sequentially along the chain, ensuring that each request is handled in a predefined order.
  • Fallback Mechanism: The chain can include a mechanism to handle requests that are not handled by any handler in the chain, providing a fallback or default behavior.
  • Variants: The pattern has variants like a linear chain, where each handler has a single successor, or a tree-like structure, where a handler can have multiple successors, allowing for more complex processing logic.
  • Enhanced Flexibility: The pattern allows for enhanced flexibility in handling requests, as the chain can be configured or modified to suit different requirements without changing the client code.

Real-World Analogy of the Chain Of Responsibility Design Pattern

Imagine a customer service department with multiple levels of support staff, each responsible for handling different types of customer inquiries based on their complexity. The chain of responsibility can be illustrated as follows:

  • Level 1 Support:
    • This represents the first point of contact for customer inquiries. Level 1 support staff handle basic inquiries and provide general assistance. If they cannot resolve the issue, they escalate it to Level 2 support.
  • Level 2 Support:
    • This level consists of more experienced support staff who can handle more complex issues that Level 1 support cannot resolve. If Level 2 support cannot resolve the issue, they escalate it to Level 3 support.
  • Level 3 Support:
    • This is the highest level of support, consisting of senior or specialized staff who can handle critical or highly technical issues. If Level 3 support cannot resolve the issue, they may involve other departments or experts within the organization.

RealWorldExampleChainOfResponsibility-(3)

Components of the Chain of Responsibility Design Pattern

The Chain of Responsibility Pattern consists of the following key components:

1. Handler Interface or Abstract Class

This is the base class that defines the interface for handling requests and, in many cases, for chaining to the next handler in the sequence.

2. Concrete Handlers

These are the classes that implement how the requests are going to be handled. They can handle the request or pass it to the next handler in the chain if it is unable to handle that request.

3. Client

The request is sent by the client, who then forwards it to the chain’s first handler. Which handler will finally handle the request is unknown to the client.

Chain of Responsibility Design Pattern Example

Imagine a customer support system where customer requests need to be handled based on their priority. There are three levels of support: Level 1, Level 2, and Level 3. Level 1 support handles basic requests, Level 2 support handles more complex requests, and Level 3 support handles critical issues that cannot be resolved by Level 1 or Level 2.

Benefit of Using the Chain of Responsibility in this scenario:

The Chain of Responsibility pattern is beneficial in this situation because it allows us to create a chain of handlers, where each handler can either handle a request or pass it to the next handler in the chain. This way, we can easily add or remove handlers without modifying the client code, providing flexibility and scalability in handling customer requests.

chainofresponsibiltydesignpatternclassdiagram-

Below is the code of above problem statement using Chain of Responsibility Pattern in Java:

Let’s break down into the component wise code:

1. Handler Interface

Defines the interface for handling requests. Includes methods for handling requests (handleRequest()) and setting the next handler in the chain (setNextHandler()).

Java
public interface SupportHandler {     void handleRequest(Request request);     void setNextHandler(SupportHandler nextHandler); } 

2. Concrete Handlers

Implement the SupportHandler interface. Each handler is responsible for handling requests based on its assigned priority level. If a handler can handle the request, it processes it; otherwise, it passes the request to the next handler in the chain.

Java
public class Level1SupportHandler implements SupportHandler {     private SupportHandler nextHandler;      public void setNextHandler(SupportHandler nextHandler) {         this.nextHandler = nextHandler;     }      public void handleRequest(Request request) {         if (request.getPriority() == Priority.BASIC) {             System.out.println("Level 1 Support handled the request.");         } else if (nextHandler != null) {             nextHandler.handleRequest(request);         }     } }  public class Level2SupportHandler implements SupportHandler {     private SupportHandler nextHandler;      public void setNextHandler(SupportHandler nextHandler) {         this.nextHandler = nextHandler;     }      public void handleRequest(Request request) {         if (request.getPriority() == Priority.INTERMEDIATE) {             System.out.println("Level 2 Support handled the request.");         } else if (nextHandler != null) {             nextHandler.handleRequest(request);         }     } }  public class Level3SupportHandler implements SupportHandler {     public void handleRequest(Request request) {         if (request.getPriority() == Priority.CRITICAL) {             System.out.println("Level 3 Support handled the request.");         } else {             System.out.println("Request cannot be handled.");         }     }      public void setNextHandler(SupportHandler nextHandler) {         // No next handler for Level 3     } } 

Complete code for the above example

Below is the complete code for the above example:

Java
// Handler Interface interface SupportHandler {     void handleRequest(Request request);     void setNextHandler(SupportHandler nextHandler); }  // Concrete Handlers class Level1SupportHandler implements SupportHandler {     private SupportHandler nextHandler;      public void setNextHandler(SupportHandler nextHandler) {         this.nextHandler = nextHandler;     }      public void handleRequest(Request request) {         if (request.getPriority() == Priority.BASIC) {             System.out.println("Level 1 Support handled the request.");         } else if (nextHandler != null) {             nextHandler.handleRequest(request);         }     } }  class Level2SupportHandler implements SupportHandler {     private SupportHandler nextHandler;      public void setNextHandler(SupportHandler nextHandler) {         this.nextHandler = nextHandler;     }      public void handleRequest(Request request) {         if (request.getPriority() == Priority.INTERMEDIATE) {             System.out.println("Level 2 Support handled the request.");         } else if (nextHandler != null) {             nextHandler.handleRequest(request);         }     } }  class Level3SupportHandler implements SupportHandler {     public void handleRequest(Request request) {         if (request.getPriority() == Priority.CRITICAL) {             System.out.println("Level 3 Support handled the request.");         } else {             System.out.println("Request cannot be handled.");         }     }      public void setNextHandler(SupportHandler nextHandler) {         // No next handler for Level 3     } }  // Request Class class Request {     private Priority priority;      public Request(Priority priority) {         this.priority = priority;     }      public Priority getPriority() {         return priority;     } }  // Priority Enum enum Priority {     BASIC, INTERMEDIATE, CRITICAL }  // Main Class public class Main {     public static void main(String[] args) {         SupportHandler level1Handler = new Level1SupportHandler();         SupportHandler level2Handler = new Level2SupportHandler();         SupportHandler level3Handler = new Level3SupportHandler();          level1Handler.setNextHandler(level2Handler);         level2Handler.setNextHandler(level3Handler);          Request request1 = new Request(Priority.BASIC);         Request request2 = new Request(Priority.INTERMEDIATE);         Request request3 = new Request(Priority.CRITICAL);          level1Handler.handleRequest(request1);         level1Handler.handleRequest(request2);         level1Handler.handleRequest(request3);     } } 
Output
Level 1 Support handled the request. Level 2 Support handled the request. Level 3 Support handled the request. 

Advantages of the Chain of Responsibility Design Pattern

  • Decoupling of Objects: The pattern makes enables sending a request to a series of possible recipients without having to worry about which object will handle it in the end. This lessens the reliance between items.
  • Flexibility and Extensibility: New handlers can be easily added or existing ones can be modified without affecting the client code. This promotes flexibility and extensibility within the system.
  • Dynamic Order of Handling: The sequence and order of handling requests can be changed dynamically during runtime, which allows adjustment of the processing logic as per the requirements.
  • Simplified Object Interactions: It simplifies the interaction between the sender and receiver objects, as the sender does not need to know about the processing logic.
  • Enhanced Maintainability: Each handler performs a specific type of processing, which making maintaining and modifying the individual components easier without impacting the overall system.

Disadvantages of the Chain of Responsibility Design Pattern

  • Possible Unhandled Requests: The chain should be implemented correctly otherwise there is a chance that some requests might not get handled at all, which leads to unexpected behavior in the application.
  • Performance Overhead: The request will go through several handlers in the chain if it is lengthy and complicated, which could cause performance overhead. The processing logic of each handler has an effect on the system’s overall performance.
  • Complexity in Debugging: The fact that the chain has several handlers can make debugging more difficult. Tracking the progression of a request and determining which handler is in charge of handling it can be difficult.
  • Runtime Configuration Overhead: It may become more difficult to manage and maintain the chain of responsibility if the chain is dynamically modified at runtime.

Conclusion

In conclusion, the Chain of Responsibility pattern is a powerful tool for creating a flexible and extensible chain of handlers to process requests. It promotes loose coupling, making it a valuable addition to your design pattern toolbox when building applications. However, like any design pattern, it should be used judiciously, considering the specific requirements of your application.
 


Next Article
Chain of Responsibility - Python Design Patterns

S

sanketsay9qs
Improve
Article Tags :
  • Design Pattern
  • System Design
  • Java Design Patterns

Similar Reads

  • Chain of Responsibility Design Pattern
    The Chain of Responsibility design pattern is a behavioral design pattern that allows an object to pass a request along a chain of handlers. Each handler in the chain decides either to process the request or to pass it along the chain to the next handler. Table of Content What is the Chain of Respon
    10 min read
  • Chain of Responsibility Method Design Pattern in Javascript
    The Chain of Responsibility design pattern in JavaScript allows a request to be passed through a chain of handlers until one processes it. This pattern helps manage complex workflows by decoupling the sender of a request from its receivers, making the system more flexible and easier to extend. In th
    6 min read
  • Chain of Responsibility Method Design Patterns in C++
    Chain of responsibility Pattern or Chain of Responsibility Method is a behavioral pattern, which allows an object to send a request to other objects without knowing who is going to handle it. This pattern is frequently used in the chain of multiple objects, where each object either handles the reque
    8 min read
  • Chain of Responsibility - Python Design Patterns
    Chain of Responsibility method is Behavioral design pattern and it is the object-oriented version of if ... elif ... elif ... else and make us capable to rearrange the condition-action blocks dynamically at the run-time. It allows us to pass the requests along the chain of handlers. The processing i
    4 min read
  • Proxy Method Design Pattern in Java
    A Proxy Method or Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. This pattern involves creating a new class, known as the proxy, which acts as an intermediary between a client and the real object. The proxy obj
    9 min read
  • Template Method Design Pattern in Java
    Template Design Pattern or Template Method is the behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. This pattern falls under the category of the "behavioral" design patter
    10 min read
  • Prototype Design Pattern in Java
    The Prototype Design Pattern in Java is a creational pattern that enables the creation of new objects by copying an existing object. Prototype allows us to hide the complexity of making new instances from the client. The concept is to copy an existing object rather than create a new instance from sc
    9 min read
  • Strategy Design Pattern in Java
    The Strategy Design Pattern in Java defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing clients to switch algorithms dynamically without altering the code structure. Important Topics for Strategy Design Pattern in Java What is the Strategy Design Pattern i
    13 min read
  • State Design Pattern in Java
    The State design pattern in Java is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. It achieves this by encapsulating the object's behavior within different state objects, and the object itself dynamically switches between these state
    10 min read
  • Command Method Design Pattern in Java
    The Command Design Pattern in Java is a behavioral design pattern that turns a request into a stand-alone object, allowing parameterization of clients with different requests, queuing of requests, and support for undoable operations(action or a series of actions that can be reversed or undone in a s
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences