Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Go Premium
  • 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:
Builder Pattern | C++ Design Patterns
Next article icon

Chain of Responsibility Method Design Patterns in C++

Last Updated : 20 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 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 how the request is handled.

When-Does-a-Distributed-System-Need-ZooKeeper-copy-4

Important Topics for the Chain of Responsibility Method Design Patterns in C++

  • Example of Chain of Responsibility Design Pattern in C++
  • Advantages of the Chain of Responsibility Pattern in C++
  • Disadvantages of the Chain of Responsibility Pattern in C++
  • Conclusion:

Example of Chain of Responsibility Design Pattern in C++

Problem Statement:

Suppose we have to handle authentication requests with a chain of handlers. Depending on the type of authentication request, the appropriate handler in the chain handles it, or it is passed along the chain until a handler can process it or until the end of the chain is reached.

Implementation of Chain of Responsibility Pattern in C++:

AuthenticationHandler (Handler Interface):

This is an abstract base class that defines the interface for all authentication handlers. It declares two pure virtual functions:

  • setNextHandler(AuthenticationHandler* handler): This function allows setting the next handler in the chain.
  • handleRequest(const std::string& request): This function is responsible for handling authentication requests.

Below is the implementation of the above function:

C++
// Handler Interface
class AuthenticationHandler {
public:
    virtual void
    setNextHandler(AuthenticationHandler* handler)
        = 0;
    virtual void handleRequest(const std::string& request)
        = 0;
};

Concrete Handlers:

Two concrete handler classes are defined:

1. UsernamePasswordHandler: This handler checks if the authentication request is for "username_password." If it is, it prints a message indicating successful authentication using a username and password. If the request is not for "username_password," it forwards the request to the next handler in the chain, if available. If there is no next handler, it prints a message indicating an invalid authentication method.

Below is the implementation of the above function:

C++
// Concrete Handlers
class UsernamePasswordHandler
    : public AuthenticationHandler {
private:
    AuthenticationHandler* nextHandler;

public:
    void
    setNextHandler(AuthenticationHandler* handler) override
    {
        nextHandler = handler;
    }

    void handleRequest(const std::string& request) override
    {
        if (request == "username_password") {
            std::cout << "Authenticated using username and "
                         "password."
                      << std::endl;
        }
        else if (nextHandler != nullptr) {
            nextHandler->handleRequest(request);
        }
        else {
            std::cout << "Invalid authentication method."
                      << std::endl;
        }
    }
};


2. OAuthHandler: This handler checks if the authentication request is for "oauth_token." If it is, it prints a message indicating successful authentication using an OAuth token. If the request is not for "oauth_token," it forwards the request to the next handler in the chain, if available. If there is no next handler, it prints a message indicating an invalid authentication method.

C++
class OAuthHandler : public AuthenticationHandler {
private:
    AuthenticationHandler* nextHandler;

public:
    void setNextHandler(AuthenticationHandler* handler) override {
        nextHandler = handler;
    }

    void handleRequest(const std::string& request) override {
        if (request == "oauth_token") {
            std::cout << "Authenticated using OAuth token." << std::endl;
        } else if (nextHandler != nullptr) {
            nextHandler->handleRequest(request);
        } else {
            std::cout << "Invalid authentication method." << std::endl;
        }
    }
};


Note: In the main function, two instances of the concrete handlers are created: usernamePasswordHandler and oauthHandler.

Main Function:

Three authentication requests are made using handleRequest:

  • First, an "oauth_token" request is sent. The request is successfully handled by the oauthHandler.
  • Second, a "username_password" request is sent. This time, the request is successfully handled by the usernamePasswordHandler.
  • Third, an "invalid_method" request is sent. None of the handlers in the chain can handle this request, so an "Invalid authentication method" message is printed.

Below is the implementation of the above function:

C++
int main()
{
    AuthenticationHandler* usernamePasswordHandler
        = new UsernamePasswordHandler();
    AuthenticationHandler* oauthHandler
        = new OAuthHandler();

    // Set up the chain
    usernamePasswordHandler->setNextHandler(oauthHandler);

    // Handling authentication requests
    usernamePasswordHandler->handleRequest("oauth_token");
    usernamePasswordHandler->handleRequest(
        "username_password");
    usernamePasswordHandler->handleRequest(
        "invalid_method");

    delete usernamePasswordHandler;
    delete oauthHandler;

    return 0;
}


Below is the complete combined code of the above example:

C++
#include <iostream>
#include <string>

// Handler Interface
class AuthenticationHandler {
public:
    virtual void
    setNextHandler(AuthenticationHandler* handler)
        = 0;
    virtual void handleRequest(const std::string& request)
        = 0;
};

// Concrete Handlers
class UsernamePasswordHandler
    : public AuthenticationHandler {
private:
    AuthenticationHandler* nextHandler;

public:
    void
    setNextHandler(AuthenticationHandler* handler) override
    {
        nextHandler = handler;
    }

    void handleRequest(const std::string& request) override
    {
        if (request == "username_password") {
            std::cout << "Authenticated using username and "
                         "password."
                      << std::endl;
        }
        else if (nextHandler != nullptr) {
            nextHandler->handleRequest(request);
        }
        else {
            std::cout << "Invalid authentication method."
                      << std::endl;
        }
    }
};

class OAuthHandler : public AuthenticationHandler {
private:
    AuthenticationHandler* nextHandler;

public:
    void
    setNextHandler(AuthenticationHandler* handler) override
    {
        nextHandler = handler;
    }

    void handleRequest(const std::string& request) override
    {
        if (request == "oauth_token") {
            std::cout << "Authenticated using OAuth token."
                      << std::endl;
        }
        else if (nextHandler != nullptr) {
            nextHandler->handleRequest(request);
        }
        else {
            std::cout << "Invalid authentication method."
                      << std::endl;
        }
    }
};

// Client
int main()
{
    AuthenticationHandler* usernamePasswordHandler
        = new UsernamePasswordHandler();
    AuthenticationHandler* oauthHandler
        = new OAuthHandler();

    // Set up the chain
    usernamePasswordHandler->setNextHandler(oauthHandler);

    // Handling authentication requests
    usernamePasswordHandler->handleRequest("oauth_token");
    usernamePasswordHandler->handleRequest(
        "username_password");
    usernamePasswordHandler->handleRequest(
        "invalid_method");

    delete usernamePasswordHandler;
    delete oauthHandler;

    return 0;
}

Output
Authenticated using OAuth token.
Authenticated using username and password.
Invalid authentication method.

Key Component of Chain of Responsibility Design Pattern in C++

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

  • 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.
  • 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.
  • 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.

Diagrammatical Representation of the Example

Untitled-(2)
Diagrammatical Representation of Authentication Handling with Chain of Responsibility Pattern in C++.


  • Setting up the Interface: The code begins with the definition of an abstract class, "AuthenticationHandler", which sets the guidelines for handling different authentication requests. It declares two virtual functions:
  • Implementing Concrete Handlers: There are Two concrete classes namely "UsernamePasswordHandler" and "OAuthHandler", implementing the "AuthenticationHandler" interface. Each of these handlers processes a specific type of authentication request.
  • Setting up the Client Code: The "main" function serves as the client code in this example.

The example effectively demonstrates the Chain of Responsibility pattern, where different authentication handlers are linked together to handle specific types of requests, providing a flexible and decoupled approach to authentication processing.

Advantages of the Chain of Responsibility Pattern in C++

  • 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 Pattern in C++

  • 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 C++ applications. However, like any design pattern, it should be used judiciously, considering the specific requirements of your application.


Next Article
Builder Pattern | C++ Design Patterns

R

rudra1807raj
Improve
Article Tags :
  • Design Pattern
  • Geeks Premier League
  • System Design
  • Geeks Premier League 2023
  • C++ Design Pattern

Similar Reads

    Modern C++ Design Patterns Tutorial
    Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
    7 min read

    Creational Software Design Patterns in C++

    Factory Method Pattern | C++ Design Patterns
    Factory Method Pattern provides an interface for creating objects but leaves the actual object instantiation to derived classes. This allows for flexibility in object creation and promotes loose coupling between the client code and the concrete products.Factory Method Pattern | C++ Design PatternsTa
    8 min read
    Abstract Factory Pattern | C++ Design Patterns
    Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are
    6 min read
    Builder Pattern | C++ Design Patterns
    The builder pattern is defined as a creational design pattern that separates the construction of a complex object from its representation, allowing us to create different representations of an object using the same construction process. It's beneficial when an object has many optional properties or
    6 min read
    Prototype Pattern | C++ Design Patterns
    When designing software, it's crucial to make it efficient, easy to reuse, and simple to maintain. One way to achieve these goals is by using design patterns, and one such pattern is the Prototype Pattern. In this article, we'll explore the Prototype Design Pattern in the context of C++. Important T
    5 min read
    Singleton Pattern | C++ Design Patterns
    A singleton pattern is a design pattern that ensures that only one instance of a class can exist in the entire program. This means that if you try to create another instance of the class, it will return the same instance that was created earlier. The Singleton pattern is useful when we need to have
    11 min read

    Structural Software Design Patterns in C++

    Adapter Pattern | C++ Design Patterns
    Adapter Pattern is a structural design pattern used to make two incompatible interfaces work together. It acts as a bridge between two incompatible interfaces, allowing them to collaborate without modifying their source code. This pattern is particularly useful when integrating legacy code or third-
    6 min read
    Bridge Method | C++ Design Patterns
    Bridge Pattern is basically a structural design pattern in software engineering or in C++ programming that is used to separate an object's abstraction from its implementation. It is part of the Gang of Four (GoF) design patterns and is particularly useful when we need to avoid a permanent binding be
    9 min read
`; $(commentSectionTemplate).insertBefore(".article--recommended"); } loadComments(); }); }); function loadComments() { if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
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