chain-of-responsibility
1. Chain of Responsibility is a behavioral design pattern.
2. The basic idea behind this pattern is that a request or a command passes through a chain of objects until it is handled.
3. Objects involved in this pattern are of two types. Processing Objects and Command Objects.
4. Processing objects handle the commands given by command objects.
5. Each processing object knows what it can handle and passes the command to the next member in the chain if not handled.
Flow diagram
Class Diagram
Example
#include<iostream>
using namespace std;
class chain;
class chain {
protected :
chain *next;
public:
void addnext(chain *obj)
{
next = obj;
}
virtual void perform(int number)=0;
};
class drawT : public chain
{
public:
void perform(int number)
{
int update = 0;
if(number == 0)
{
cout<<"completed "<<endl;
return;
}
update = number%1000;
number = number/1000;
if(number>=1)
cout<<"number of 1000's : "<<number<<endl;
if(update == 0)
cout<<"Done"<<endl;
else
next->perform(update);
}
};
class drawF : public chain
{
public:
void perform(int number)
{
int update = number % 500;
number = number/500;
if(number>=1)
cout<<"number of 500's : "<<number<<endl;
if(update == 0)
cout<<"Done"<<endl;
else
next->perform(update);
}
};
class drawH : public chain
{
public:
void perform(int number)
{
int update = number % 100;
number = number/100;
if(number)
cout<<"number of 100's : "<<number<<endl;
}
};
int main()
{
chain *start = new drawT();
chain *five = new drawF();
chain *Hundred = new drawH();
start->addnext(five);
five->addnext(Hundred);
int number;
cout<<"Enter the number : ";
cin>>number;
start->perform(number);
return 0;
}
2. The basic idea behind this pattern is that a request or a command passes through a chain of objects until it is handled.
3. Objects involved in this pattern are of two types. Processing Objects and Command Objects.
4. Processing objects handle the commands given by command objects.
5. Each processing object knows what it can handle and passes the command to the next member in the chain if not handled.
Flow diagram
Class Diagram
Example
#include<iostream>
using namespace std;
class chain;
class chain {
protected :
chain *next;
public:
void addnext(chain *obj)
{
next = obj;
}
virtual void perform(int number)=0;
};
class drawT : public chain
{
public:
void perform(int number)
{
int update = 0;
if(number == 0)
{
cout<<"completed "<<endl;
return;
}
update = number%1000;
number = number/1000;
if(number>=1)
cout<<"number of 1000's : "<<number<<endl;
if(update == 0)
cout<<"Done"<<endl;
else
next->perform(update);
}
};
class drawF : public chain
{
public:
void perform(int number)
{
int update = number % 500;
number = number/500;
if(number>=1)
cout<<"number of 500's : "<<number<<endl;
if(update == 0)
cout<<"Done"<<endl;
else
next->perform(update);
}
};
class drawH : public chain
{
public:
void perform(int number)
{
int update = number % 100;
number = number/100;
if(number)
cout<<"number of 100's : "<<number<<endl;
}
};
int main()
{
chain *start = new drawT();
chain *five = new drawF();
chain *Hundred = new drawH();
start->addnext(five);
five->addnext(Hundred);
int number;
cout<<"Enter the number : ";
cin>>number;
start->perform(number);
return 0;
}


Comments
Post a Comment