Java responsibility chain mode template code sharing, java responsibility template sharing
This article shares a Java programming responsibility chain pattern template code with detailed notes for your reference. The details are as follows:
// Abstract Handler public abstract class Handler {private Handler nextHandler; // each Handler must process the Request public final Response handleMessage (request Request) {Response response = null; // determine whether the processing level is self-defined if (this. getHandlerLevel (). equals (request. getRequestLevel () {response = this. echo (request);} else {// determine whether there is a next processor if (this. nextHandler! = Null) {response = this. nextHandler. handleMessage (request);} else {// no proper Handler} return response;} // set the public void setNext (handler _ Handler) {this. nextHandler = _ handler;} // each handler has a processing Level protected abstract Level getHandlerLevel (); // each handler must implement the processing task protected abstract Response echo (Request request );} // specific Handler 1 publlic class ConcreteHandler1 extends Handler {// define your own processing logic protected Response echo (Request request) {// complete processing logic return null ;} // set your processing Level protected Level getHandlerLevel () {// set your processing Level return null ;}} // specific Handler 2 publlic class ConcreteHandler2 extends Handler {// define your own processing logic protected Response echo (Request request) {// complete processing logic return null ;} // set your processing Level protected Level getHandlerLevel () {// set your processing Level return null ;}} // specific Handler 3 publlic class ConcreteHandler3 extends Handler {// define your own processing logic protected Response echo (Request request) {// complete processing logic return null ;} // set your processing Level protected Level getHandlerLevel () {// set your processing Level return null ;}} // code about the framework in the mode public class Level {// defines a Request and processing Level} public class Request {// public Level getRequestLevel () {return null ;}} public class Response {// process the returned data} // scenario class public class Client {public static void main (String [] args) {// declare all processing nodes Handler handler1 = new ConcreteHandler1 (); Handler handler2 = new ConcreteHandler2 (); Handler handler2 = new ConcreteHandler3 (); // set the phase sequence in the chain 1 --> 2 --> 3 handler1.setNext (handler2); handler2.setNext (handler3); // submit the request Response response = handler. handleMessage (new Request ());}}
Summary
The above is all the content that this article shares with you about the Java responsibility chain pattern template code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!