Java Design Pattern Application--responsibility chain model

Source: Internet
Author: User

It is very good to use the responsibility chain model to produce a product that needs to perform multiple steps in turn.

During the development of the performance alarm module, it is necessary to create an alarm rule to perform many operations such as threshold resolution, intermediate table generation, Flow task generation, rule warehousing, and alarm event warehousing. If you combine these steps in a class, code readability and complexity are often disastrous, especially for transactional operations with so many steps. Using the responsibility chain model, the above problems are solved.

To create an alarm rule as an example, simplify the process as follows

Threshold Resolution---> Flow task Generation---> Rule warehousing

The rollback process is as follows

1. Threshold resolution failure: rollback threshold resolution.

2, stream task production failure: rollback Flow task generation, threshold resolution.

3, Rule storage failure: rollback rule Inbound, Flow task generation, threshold resolution.

Using the responsibility chain model coding, the idea is as follows:

1, write the threshold resolution processor, stream task generation processor, rule inbound processor, each processor contains business processing method and rollback method;

2, a processor business code after the completion of the active call to the next processor business method;

3, a processor business code execution failed to actively call this processor rollback method, the processor rollback completes the active call to the previous processor rollback method.

The code is as follows

1. Abstract processor

 PackageCom.coshaho.learn.handler;/*** * Abstractrulehandler.java Create on May 5, 2017 11:20:15 * class function Description: Alarm rule Responsibility chain processing node abstract class * * Copyright:copyrig HT (c) * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public Abstract classAbstractrulehandler {//Previous Processor    PrivateAbstractrulehandler Prehandler; //Next Processor    PrivateAbstractrulehandler Nexthandler; /*** Business Execution * *@authorCoshaho *@paramRule*/     Public voiddohandle (Alarmrule rule) {Try{dohandlereal (rule); }        Catch(Exception e) {//business code execution failed active rollbackrollBack (rule); }                //business code execution successfully invokes the next processor processing        if(NULL!=Nexthandler)        {nexthandler.dohandle (rule); }    }        /*** Transaction Rollback * *@authorCoshaho *@paramRule*/     Public voidrollBack (Alarmrule rule) {rollbackreal (rule); //This processor business rollback is completed, the active call to the previous processor service rollback        if(NULL!=Prehandler)        {prehandler.rollback (rule); }    }        /*** Business processing methods specific to each processor *@authorCoshaho *@paramRule *@throwsException*/     Public Abstract voidDohandlereal (Alarmrule rule)throwsException; /*** Service-specific rollback method per processor * *@authorCoshaho *@paramRule*/     Public Abstract voidrollbackreal (alarmrule rule); PrivateAbstractrulehandler Setprehandler (Abstractrulehandler prehandler) { This. Prehandler =Prehandler; returnPrehandler; }     PublicAbstractrulehandler Setnexthandler (Abstractrulehandler nexthandler) { This. Nexthandler =Nexthandler; Nexthandler.setprehandler ( This); returnNexthandler; }}

2. Threshold resolution Processor

 PackageCom.coshaho.learn.handler;Importorg.apache.commons.lang.StringUtils;/*** * Thresholdparsehandler.java Create on May 5, 2017 11:41:20 * class function Description: Threshold Resolution * * COPYRIGHT:COPYRIGHT (c) * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public classThresholdparsehandlerextendsabstractrulehandler{@Override Public voidDohandlereal (Alarmrule rule)throwsException {if(Stringutils.isempty (Rule.getthreshold ())) {Throw NewException ("Threshold is empty.")); } System.out.println ("Parse threshold success. Threshold is "+rule.getthreshold ()); } @Override Public voidrollbackreal (Alarmrule rule) {System.out.println ("Roll parse threshold." Threshold is "+rule.getthreshold ()); }}

3. Stream Task Generation processor

 PackageCom.coshaho.learn.handler;/*** * Streamgeneratehandler.java Create on May 5, 2017 11:41:43 * class function Description: Alarm Flow rule Generation * * Copyright:copyright ( c) * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public classStreamgeneratehandlerextendsAbstractrulehandler {@Override Public voidDohandlereal (Alarmrule rule)throwsException {System.out.println ("Generate stream success."); } @Override Public voidrollbackreal (Alarmrule rule) {System.out.println ("Roll Generate stream."); }}

4. Regular inbound processor

 PackageCom.coshaho.learn.handler;Importorg.apache.commons.lang.StringUtils;/*** * Rulepesisthandler.java Create on May 5, 2017 11:41:08 * class function Description: Alarm rule Persistence * * Copyright:copyright (c) 2 013 * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public classRulepesisthandlerextendsAbstractrulehandler {@Override Public voidDohandlereal (Alarmrule rule)throwsException {if(Stringutils.isempty (Rule.getname ())) {Throw NewException ("Rule name is empty.")); } System.out.println ("Persist rule success. Rule name is "+rule.getname ()); } @Override Public voidrollbackreal (Alarmrule rule) {System.out.println ("Roll persist rule." Rule name is "+rule.getname ()); }}

5. Regular inbound processor

 PackageCom.coshaho.learn.handler;Importorg.apache.commons.lang.StringUtils;/*** * Rulepesisthandler.java Create on May 5, 2017 11:41:08 * class function Description: Alarm rule Persistence * * Copyright:copyright (c) 2 013 * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public classRulepesisthandlerextendsAbstractrulehandler {@Override Public voidDohandlereal (Alarmrule rule)throwsException {if(Stringutils.isempty (Rule.getname ())) {Throw NewException ("Rule name is empty.")); } System.out.println ("Persist rule success. Rule name is "+rule.getname ()); } @Override Public voidrollbackreal (Alarmrule rule) {System.out.println ("Roll persist rule." Rule name is "+rule.getname ()); }}

6. Alarm Rules

 PackageCom.coshaho.learn.handler;/*** * Alarmrule.java Create on May 5, 2017 11:40:50 * class function Description: Alarm rule * * Copyright:copyright (c) * Compa Ny:coshaho * @Version 1.0 * @Author Coshaho*/ Public classAlarmrule {PrivateString name; PrivateString type; PrivateString threshold;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetType () {returntype; }     Public voidsetType (String type) { This. Type =type; }     PublicString Getthreshold () {returnthreshold; }     Public voidSetthreshold (String threshold) { This. Threshold =threshold; }}

7, the rule creates the responsibility chain

 PackageCom.coshaho.learn.handler;/*** * Alarmrulecreator.java Create on May 5, 2017 11:56:45 * class function Description: Alarm rule Creation * * Copyright:copyright (c) 201 3 * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public classAlarmrulecreator {PrivateAbstractrulehandler Alarmrulehandler;  PublicAlarmrulecreator () {Alarmrulehandler=NewThresholdparsehandler (); Alarmrulehandler.setnexthandler (NewStreamgeneratehandler ()). Setnexthandler (NewRulepesisthandler ()); }         Public voidCreate (Alarmrule rule) {alarmrulehandler.dohandle (rule); }     Public Static voidMain (string[] args) {Alarmrule rule=NewAlarmrule (); Rule.setthreshold ("Cpurate < 10"); Rule.setname ("Cpu Alarm"); Alarmrulecreator Rulecreator=NewAlarmrulecreator ();        rulecreator.create (rule);                System.out.println (); Rule.setname ("");    rulecreator.create (rule); }}

Test results

Parse threshold success. Threshold is Cpurate <<< 10

Java Design Pattern Application--responsibility chain model

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.