Open In App

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

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:

Below is the implementation of the above function:




// 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:




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




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:

Below is the implementation of the above function:




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:




#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:

Diagrammatical Representation of the Example

Diagrammatical Representation of Authentication Handling with Chain of Responsibility Pattern in C++.

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++

Disadvantages of the Chain of Responsibility Pattern in C++

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.


Article Tags :