Chain of Responsibility (Responsibility Chain) java Implementation

Source: Internet
Author: User
Package ChainofResponsibility. myTest; public class MyTest {/** Chain of Responsibility (Responsibility Chain) * @ param args */public static void main (String [] args) {AbstractRequest ar1 = new CookRequest ("issuing cooking command"); AbstractRequest ar2 = new EatRequest ("issuing meal command"); AbstractRequest ar3 = new WishRequest ("issuing dishwashing command "); export acthandler ah1 = new CookHandler (); export acthandler ah2 = new EatHandler (); export acthandler ah3 = new WishHandler (); extract (ah2); extract (ah3); ah1.Handler (ar1 ); // ah1.HandlerRequest (ar3 );}}

package ChainofResponsibility.MyTest;public enum Level {LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4, LEVEL_5, LEVEL_6, LEVEL_7;private  int reutrnLevel(Level rl) {switch (rl) {case LEVEL_1:return 1;case LEVEL_2:return 2;case LEVEL_3:return 3;case LEVEL_4:return 4;case LEVEL_5:return 5;case LEVEL_6:return 6;case LEVEL_7:return 7;}return 0;}public byte CompareSuper(Level rl){if(reutrnLevel(this)>reutrnLevel(rl)){return 1;}else if(reutrnLevel(this)==reutrnLevel(rl)){return 0;}else{return -1;}}}

Package ChainofResponsibility. myTest; public abstract class extends acthandler {extends acthandler nexthandler; public void setNextRequest (AbstractHandler nexthandler) {this. nexthandler = nexthandler;}/** process the request in a certain way **/public final void HandlerRequest (AbstractRequest request) {if (request. getRequestLevel () = this. getHandlerLevel () {this. handler (request); return;} else {if (nexthandler! = Null) {if (request. getRequestLevel (). compareSuper (this. nexthandler. getHandlerLevel () = 1) {System. out. println ("method cannot be executed directly" + request. getContent () + ", you must first execute the previous task: \ n" + this. nexthandler. getContent (); this. nexthandler. handlerRequest (request);} else if (request. getRequestLevel (). compareSuper (this. nexthandler. getHandlerLevel () = 0) {System. out. println ("executable method:"); this. nexthandler. handler (request);} else {System. out. println ("non-logical task") ;}} else {if (request. getRequestLevel (). compareSuper (this. getHandlerLevel () =-1) {System. out. println ("non-logical task");} else {System. out. println ("unable to complete the task") ;}}} public abstract String getContent (); public abstract Level getHandlerLevel (); /** specific request method **/public abstract void Handler (AbstractRequest request );}

Package ChainofResponsibility. myTest; public class CookHandler extends acthandler {@ Overridepublic void Handler (AbstractRequest request) {System. out. println ("Cooking") ;}@ Overridepublic Level getHandlerLevel () {return Level. LEVEL_1 ;}@ Overridepublic String getContent () {// TODO Auto-generated method stubreturn "Cooking ";}}

Package ChainofResponsibility. myTest; public class EatHandler extends acthandler {@ Overridepublic void Handler (AbstractRequest request) {// TODO Auto-generated method stubSystem. out. println ("eating") ;}@ Overridepublic Level getHandlerLevel () {// TODO Auto-generated method stubreturn Level. LEVEL_2 ;}@ Overridepublic String getContent () {// TODO Auto-generated method stubreturn "eating ";}}

Package ChainofResponsibility. myTest; public class WishHandler extends acthandler {@ Overridepublic void Handler (AbstractRequest request) {System. out. println ("washing dishes") ;}@ Overridepublic Level getHandlerLevel () {return Level. LEVEL_3 ;}@ Overridepublic String getContent () {// TODO Auto-generated method stubreturn "washing dishes ";}}

package ChainofResponsibility.MyTest;public abstract class AbstractRequest {String content;public AbstractRequest(String content) {this.content = content;}public abstract Level getRequestLevel();public String getContent() {return content;}}

package ChainofResponsibility.MyTest;public class CookRequest extends AbstractRequest {public CookRequest(String content) {super(content);}@Overridepublic Level getRequestLevel() {return Level.LEVEL_1;}}

package ChainofResponsibility.MyTest;public class EatRequest extends AbstractRequest {public EatRequest(String content) {super(content);}@Overridepublic Level getRequestLevel() {return Level.LEVEL_2;}}

package ChainofResponsibility.MyTest;public class WishRequest extends AbstractRequest {public WishRequest(String content) {super(content);}@Overridepublic Level getRequestLevel() {return Level.LEVEL_3;}}

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.