Java responsibility chain mode template code sharing, java responsibility template sharing

Source: Internet
Author: User

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!

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.