Design Pattern C ++ description__ chain of responsibility pattern

Source: Internet
Author: User
Tags ranges

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.