Learning Netty Framework, see someone said Netty used the design mode of responsibility chain mode, learning about the responsibility chain mode, mainly refer to the big talk design mode.
Main scenarios:
Side dishes want a raise, ask the manager for a raise, the manager has no authority, the manager is assigned to the Director, the Director is not authorized to deal with the general manager, finally, the general manager to deal with, do not agree.
The responsibility chain means that if there is no permission to process the request, the object with the higher permission is processed. In turn
In this case, the object is abstracted as handler class, manager, director, etc. to inherit handler's concrete processing class, while simulating client clients making requests to handler object
The class diagram is as follows
The following is a specific code implementation:
Handler.java
1 /**2 * Abstract Processing class3 * @authorGXF4 *5 */6 Public Abstract classHandler {7 PublicHandler successor;//successors, or higher-privileged objects8 Public Abstract voidHandleRequest (intrequest);//Handling client Requests9 Ten Public voidSetsuccesor (Handler successor) {//set successors, or higher-privileged objects One This. successor =successor; A } - -}
Concretehandler1.java
1 /**2 * Specific processing Class 13 * Processing requests from the request range between 1~104 * @authorGXF5 *6 */7 Public classConcreteHandler1extendsHandler {8 9 @OverrideTen Public voidHandleRequest (intrequest) { One if(Request >=0 && request <= 10) {//within the limits of his own authority, handle ASystem.out.println ("ConcreteHandler1 Handle request =" +request); - } - Else if(Successor! =NULL){//exceed their own permissions, if there is a higher permission object, leave it to handle the successor.handlerequest (request); - } - - } + -}
Concretehandler2.java
1 /**2 * Specific processing class3 * Processing reqeuest 10~20 range4 * @authorGXF5 *6 */7 Public classConcreteHandler2extendsHandler {8 9 @OverrideTen Public voidHandleRequest (intrequest) { One if(Request > && request <= 20) {//handling requests within a permission range ASystem.out.println ("ConcreteHandler2 Handle request =" +request); - } - Else if(Successor! =NULL){//requests that cannot be processed are handled by higher-privileged processing objects the successor.handlerequest (request); - } - } - +}
Concretehandler3.java
1 /**2 * Specific processing Class 33 * Processing Request range: 20~304 * @authorGXF5 *6 */7 Public classConcreteHandler3extendsHandler {8 9 @OverrideTen Public voidHandleRequest (intrequest) { One if(Request > && request <= 30){ ASystem.out.println ("ConcreteHandler3 Handle request =" +request); - } - Else if(Successor! =NULL){ the successor.handlerequest (request); - } - - } + -}
Client.java
1 /**2 * Responsibility chain mode Client3 * @authorGXF4 *5 */6 Public classClient {7 8 Public Static voidMain (String args[]) {9 //to generate a handler object for three permissionsTenHandler H1 =NewConcreteHandler1 (); OneHandler H2 =NewConcreteHandler2 (); AHandler H3 =NewConcreteHandler3 (); - - //set up a successor to the responsibility chain the h1.setsuccesor (H2); - H2.setsuccesor (H3); - - //generate 1-30 of numbers, as a request, to handler processing + intRequests[] =New int[30]; - + //initializing the reqeusts array A for(inti = 0; i < requests.length; i++){ atRequests[i] =i; - } - - for(inti = 0; i < requests.length; i++){ - h1.handlerequest (Requests[i]); -}// for in - } to +}
Responsibility chain mode of design pattern (Java implementation)