Link addresses in other modes:Click Open Link
I. Overview
Responsibility Chain Mode:
So that multiple objects have the opportunity to process the request, so as to avoid coupling between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it.
Ii. Example
Employee requirement for salary increase
There are three levels of managers in the company: General Manager, Director, and manager. If an employee asks for a salary increase, apply to the manager in charge. If the number of salary increases is within the Manager's authority, the manager can approve the application directly. Otherwise, the application will be handed over to the Director. The director can handle all requests in the same way. This is a typical responsibility chain model. The request processing forms a chain until there is an object processing request.
The structure is as follows:
Hypothesis:
The manager can handle salary ranges from 0 ~ 500
The supervisor can handle salary ranges from 500 ~ 1000
The general manager can handle salary ranges from 1000 ~ 2000
The Code is as follows:
[CPP] // /////////////////////////////////////
// Abstract processing class
Class handle
{
Public:
Virtual ~ Handle ()
{
Delete _ succ;
}
Virtual void handlerequest (INT request) = 0;
// Set its superiors
Void setsuccessor (handle * succ)
{
_ Succ = succ;
}
Handle * getsuccessor ()
{
Return _ succ;
}
Protected:
Handle ()
{
_ Succ = NULL;
}
PRIVATE:
Handle * _ succ;
};
// Processing Class
Class concretehandlea: public handle
{
Public:
Void handlerequest (INT request)
{
If (request> = 0 & request <500)
{
Cout <"concretehandlea deal with:" <request <Endl;
}
Else if (this-> getsuccessor ()! = NULL)
{
This-> getsuccessor ()-> handlerequest (request );
}
Else
{
Cout <"can't deal with" <request <Endl;
}
}
};
// Processing Class B
Class concretehandleb: public handle
{
Public:
Void handlerequest (INT request)
{
If (request >=500 & request <1000)
{
Cout <"concretehandleb deal with:" <request <Endl;
}
Else if (this-> getsuccessor ()! = NULL)
{
This-> getsuccessor ()-> handlerequest (request );
}
Else
{
Cout <"can't deal with" <request <Endl;
}
}
};
// Processing class C
Class concretehandlec: public handle
{
Public:
Void handlerequest (INT request)
{
If (request >=1000 & request <2000)
{
Cout <"concretehandlec deal with:" <request <Endl;
}
Else if (this-> getsuccessor ()! = NULL)
{
This-> getsuccessor ()-> handlerequest (request );
}
Else
{
Cout <"can't deal with" <request <Endl;
}
}
};
//////////////////////////////////////// //////////////////////////////////
// Test
Int main ()
{
Handle * H1 = new concretehandlea ();
Handle * H2 = new concretehandleb ();
Handle * h3 = new concretehandlec ();
// Set its superiors
H1-> setsuccessor (H2 );
H2-> setsuccessor (H3 );
H1-> handlerequest (300 );
H1-> handlerequest (600 );
H1-> handlerequest (1500 );
H1-> handlerequest (3000 );
Delete H1;
Delete H2;
Delete H3;
Return 0;
}
//////////////////////////////////////// //////////////////////////////////
// Abstract processing class
Class handle
{
Public:
Virtual ~ Handle ()
{
Delete _ succ;
}
Virtual void handlerequest (INT request) = 0;
// Set the upper-level www.2cto.com
Void setsuccessor (handle * succ)
{
_ Succ = succ;
}
Handle * getsuccessor ()
{
Return _ succ;
}
Protected:
Handle ()
{
_ Succ = NULL;
}
PRIVATE:
Handle * _ succ;
};
// Processing Class
Class concretehandlea: public handle
{
Public:
Void handlerequest (INT request)
{
If (request> = 0 & request <500)
{
Cout <"concretehandlea deal with:" <request <Endl;
}
Else if (this-> getsuccessor ()! = NULL)
{
This-> getsuccessor ()-> handlerequest (request );
}
Else
{
Cout <"can't deal with" <request <Endl;
}
}
};
// Processing Class B
Class concretehandleb: public handle
{
Public:
Void handlerequest (INT request)
{
If (request >=500 & request <1000)
{
Cout <"concretehandleb deal with:" <request <Endl;
}
Else if (this-> getsuccessor ()! = NULL)
{
This-> getsuccessor ()-> handlerequest (request );
}
Else
{
Cout <"can't deal with" <request <Endl;
}
}
};
// Processing class C
Class concretehandlec: public handle
{
Public:
Void handlerequest (INT request)
{
If (request >=1000 & request <2000)
{
Cout <"concretehandlec deal with:" <request <Endl;
}
Else if (this-> getsuccessor ()! = NULL)
{
This-> getsuccessor ()-> handlerequest (request );
}
Else
{
Cout <"can't deal with" <request <Endl;
}
}
};
//////////////////////////////////////// //////////////////////////////////
// Test
Int main ()
{
Handle * H1 = new concretehandlea ();
Handle * H2 = new concretehandleb ();
Handle * h3 = new concretehandlec ();
// Set its superiors
H1-> setsuccessor (H2 );
H2-> setsuccessor (H3 );
H1-> handlerequest (300 );
H1-> handlerequest (600 );
H1-> handlerequest (1500 );
H1-> handlerequest (3000 );
Delete H1;
Delete H2;
Delete H3;
Return 0;
}
Iii. Description
1. The feature of the responsibility chain is that when a customer submits a request, the request is passed along the chain until a concretehandler object is responsible for processing it.
2. The benefit of the responsibility chain is that the requester does not need to care about which object to process, and the request will be processed anyway. It only requires one successor.
3. It should be noted that a request to the chain may not be processed at the end, so it must be properly configured.
Author lwbeyond