Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Created December 31, 2017 16:44
Show Gist options
  • Save FONQRI/6e32fbbdbbdc461e02efbad68f1f0f92 to your computer and use it in GitHub Desktop.
Save FONQRI/6e32fbbdbbdc461e02efbad68f1f0f92 to your computer and use it in GitHub Desktop.
Chain of Responsibility
#include <iostream>
using namespace std;
// STATES
enum ErrorStates { ANALYZE = 0, FIX, VERIFY, CLOSE };
// Command Class
class ErrorReport {
private:
ErrorStates state;
public:
ErrorReport(ErrorStates state) { this->state = state; }
ErrorStates GetState() { return state; }
void SetState(ErrorStates state) { this->state = state; }
};
// General base class for all processing objects
class Error {
protected:
ErrorStates state;
Error *successor;
public:
virtual ~Error();
Error(ErrorStates aState) { state = aState; }
void SetSuccessor(Error *error) { this->successor = error; }
virtual void ProcessError(ErrorReport &report) = 0;
};
Error::~Error() {}
// Processing object class 1 for state ANALYZE
class AnalyzeError : public Error {
public:
AnalyzeError() : Error(ANALYZE) {}
~AnalyzeError();
void ProcessError(ErrorReport &report)
{
if (report.GetState() == ANALYZE) {
cout << "AnalyzeError::Handled the command to analyze the error ..."
<< endl;
}
else {
cout << "AnalyzeError::Passing to my successor ..." << endl;
successor->ProcessError(report);
}
}
};
AnalyzeError::~AnalyzeError() {}
// Processing object class 2 for state FIX
class FixError : public Error {
public:
FixError() : Error(FIX) {}
~FixError();
void ProcessError(ErrorReport &report)
{
if (report.GetState() == FIX) {
cout << "FixError::Handled the command to fix the error ..."
<< endl;
}
else {
cout << "FixError::Passing to my successor ..." << endl;
successor->ProcessError(report);
}
}
};
FixError::~FixError() {}
// Processing object class 3 for state VERIFY
class VerifyError : public Error {
public:
VerifyError() : Error(VERIFY) {}
~VerifyError();
void ProcessError(ErrorReport &report)
{
if (report.GetState() == VERIFY) {
cout << "VerifyError::Handled the command to verify the error ..."
<< endl;
}
else {
cout << "VerifyError::Passing to my successor ..." << endl;
successor->ProcessError(report);
}
}
};
VerifyError::~VerifyError() {}
// Processing object class 4 for state CLOSE
class CloseError : public Error {
public:
CloseError() : Error(CLOSE) {}
~CloseError();
void ProcessError(ErrorReport &report)
{
if (report.GetState() == CLOSE) {
cout << "CloseError::Handled the command to close the error ..."
<< endl;
}
else {
cout << "VerifyError::No successor ... ignore" << endl;
cout << "No action required ..." << endl;
}
}
};
CloseError::~CloseError() {}
int main()
{
// Create instances for processing objects
AnalyzeError analyzeError;
FixError fixError;
VerifyError verifyError;
CloseError closeError;
// Create the chain of responsibility
analyzeError.SetSuccessor(&fixError);
fixError.SetSuccessor(&verifyError);
verifyError.SetSuccessor(&closeError);
// Issue command 1
cout << "--------------- o/p for command 1 ----------------" << endl;
ErrorReport errorReport1(ANALYZE);
analyzeError.ProcessError(errorReport1);
// Issue command 2
cout << "--------------- o/p for command 2 ----------------" << endl;
ErrorReport errorReport2(CLOSE);
analyzeError.ProcessError(errorReport2);
// Cleanup
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment