Skip to content

Commit

Permalink
chainOfResponsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
HendEmad committed May 24, 2024
1 parent ed643f2 commit 6668eef
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Behavioral/ChainOfResponsibility/BaseHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <memory>
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\JobApplication.h"
using namespace std;

#ifndef BASE_HANDLER_H
#define BASE_HANDLER_H

class BaseHandler {
protected:
shared_ptr<BaseHandler> nextHandler;

public:
void setNext(shared_ptr<BaseHandler> handler) {
nextHandler = handler;
}

virtual void HandleRequest(shared_ptr<JobApplication> request) = 0;

virtual ~BaseHandler() = default;
};

#endif // BASE_HANDLER_H
25 changes: 25 additions & 0 deletions Behavioral/ChainOfResponsibility/Client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <memory>
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\BaseHandler.h"
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\HRHandler.h"
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\JobApplication.h"
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\TechHandler.h"
using namespace std;

int main() {
shared_ptr<JobApplication> jobApp = make_shared<JobApplication>();
jobApp -> ApplicantName = "Hend";
jobApp -> JobTitle = "SDE";
jobApp -> JobCode = "#122524";

shared_ptr<HRHandler> hr = make_shared<HRHandler>();
shared_ptr<TechHandler> tech = make_shared<TechHandler>();

hr -> setNext(tech); // create the chain

hr -> HandleRequest(jobApp); // start the chain


cout << jobApp -> showComments();

}
Binary file added Behavioral/ChainOfResponsibility/Client.exe
Binary file not shown.
58 changes: 58 additions & 0 deletions Behavioral/ChainOfResponsibility/HRHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\BaseHandler.h"
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\JobApplication.h"
#include <memory>

#ifndef HR_HANDLER_H
#define HR_HANDLER_H

class HRHandler : public BaseHandler {
public:
void toNext_Scenario3(shared_ptr <JobApplication> request) {
if(nextHandler != nullptr) {
nextHandler -> HandleRequest(request);
}
else {
request -> comments << "End by HR...\n";
}
}

void HandleRequest(shared_ptr<JobApplication> request) override{

#pragma region Scenario 1
// request -> comments << "HR evaluation comment.\n";

// if(nextHandler != nullptr) {
// nextHandler -> HandleRequest(request);
// }
// else {
// request -> comments << "End by HR Handler\n";
// }
#pragma endregion

#pragma region Scenario 2
// check if the handler is able to process the request or not
// if (request -> JobCode == "#1546") {
// request -> comments << "HR Comment...\n";
// }
// else{ // send request to the next handler
// if (nextHandler != nullptr) {
// nextHandler -> HandleRequest(request);
// }
// else {
// request -> comments << "End by HR Handler...\n";
// }
// }
#pragma endregion

if (request -> JobCode == "#1546") {
request -> comments << "HR Comment...\n";
toNext_Scenario3(request);
}
else {
toNext_Scenario3(request);
}
}

};

#endif // HR_HANDLER_H
29 changes: 29 additions & 0 deletions Behavioral/ChainOfResponsibility/JobApplication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <string>
#include <sstream>
using namespace std;

#ifndef JOB_APPLICATION_H
#define JOB_APPLICATION_H

class JobApplication {
public:
string ApplicantName;
string JobTitle;
string JobCode;
stringstream comments;

string getComments() const {
return comments.str();
}

void setComments(const string& newComments) {
comments.str("");
comments << newComments;
}

string showComments() const {
return comments.str();
}
};

#endif // JOB_APPLICATION_H
57 changes: 57 additions & 0 deletions Behavioral/ChainOfResponsibility/TechHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\BaseHandler.h"
#include "C:\Users\Data-DCS\Design-Patterns\Behavioral\ChainOfResponsibility\JobApplication.h"
#include <sstream>


#ifndef TECH_HANDLER_H
#define TECH_HANDLER_H

class TechHandler : public BaseHandler {
public:
void toNext_Scenario3(shared_ptr <JobApplication> request) {
if (nextHandler != nullptr) {
nextHandler -> HandleRequest(request);
}
else {
request -> comments << "End by tech...\n";
}
}

void HandleRequest(shared_ptr<JobApplication> request) override{

#pragma region Scenario 1
// request -> comments << "Tech evaluation comment.\n";

// if(nextHandler != nullptr) {
// nextHandler -> HandleRequest(request);
// }
// else{
// request -> comments << "End by technical handler\n";
// }
#pragma endregion

#pragma region Scenario 2
// if (request -> JobCode == "#122524") {
// request -> comments << "Tech Comment...\n";
// }
// else{
// if(nextHandler != nullptr) {
// nextHandler -> HandleRequest(request);
// }
// else {
// request -> comments << "End by tech handler...\n";
// }
// }
#pragma endregion

if(request -> JobCode == "#122524") {
request -> comments << "Tech comment...\n";
toNext_Scenario3(request);
}
else {
toNext_Scenario3(request);
}
}
};

#endif // TECH_HANDLER_H
File renamed without changes.
File renamed without changes.

0 comments on commit 6668eef

Please sign in to comment.