How to optimize the two-part repetitive code in a class

Source: Internet
Author: User

If you have two parts of the same code in a method, only the individual parameters are different, then we will take the same code out as another private method, and then in the original method to call to avoid the problem of local variables.

1  Public classDutyautowarningnotifyextendsTaskvo {2 3     /**4      * 5      */6     Private Static Final LongSerialversionuid = 4134338525795435287L;7 8     /**9      * {@inheritDoc}Ten      */ One @Override A      Public voidExecute () throwsException { -Imoncommonoperatebo Moncommonoperatebo = Springcontextutils.getbean ("Moncommonoperatebo", IMonCommonOperateBO.class); -      theString s1=moncommonoperatebo.getdictionaryitemvalue ("Idd_mon_schedule", "idi_schedule_time1"); -String s2=moncommonoperatebo.getdictionaryitemvalue ("Idd_mon_schedule", "idi_schedule_time2"); -String s3=moncommonoperatebo.getdictionaryitemvalue ("Idd_mon_schedule", "Idi_schedule_time3"); -String s4=moncommonoperatebo.getdictionaryitemvalue ("Idd_mon_schedule", "Idi_schedule_time4"); +Date time1=NewDate (System.currenttimemillis () +integer.parseint (S1));//Current time + 50 minutes -Date time2=NewDate (System.currenttimemillis () +integer.parseint (S2));//Current time + 70 minutes +Date time3=NewDate (System.currenttimemillis () +integer.parseint (S3));//Current time + 24 hours, one day advance notice time ADate time4=NewDate (System.currenttimemillis () +integer.parseint (S4));//Current time + 24 hours, one day advance notice time at         This  . SendMessage (time1, time2);  -  This . SendMessage (Time3, time4);           -     } -      -      -     Private void SendMessage(Date starttime,date EndTime)throwsexception{ inIschdutydetailbo Schdutydetailbo = Springcontextutils.getbean ("Schdutydetailbo", Ischdutydetailbo.class);  -Imoncommonoperatebo Moncommonoperatebo = Springcontextutils.getbean ("Moncommonoperatebo", IMonCommonOperateBO.class); toIuserbi Userbi = Springcontextutils.getbean ("Ext_baf_userbi", Iuserbi.class); +Schdutydetailexample schexample =Newschdutydetailexample (); -Schdutydetailexample.criteria Schcriteria =Schexample.createcriteria ();  theStringBuffer SB =NewStringBuffer (); *Sb.append ("(t_sch_duty_detail. is_notify = ' Y ' OR EXISTS (select 1"). Append ("from t_sch_duty_table SC6 WHERE t_sch_duty_d ETAIL. duty_table_id = SC6. duty_table_id ") $. Append ("and sc6.is_notify = ' Y ')");Panax NotoginsengString querycondition=sb.tostring ();  - schcriteria.andothercondition (querycondition); the Schcriteria.anddutystarttimebetween (StartTime, endTime); +list<schdutydetail> schdutydetails = schdutydetailbo.selectbyexample (schexample);//query to the corresponding shift A          for(Schdutydetail schdutydetail:schdutydetails) { theString userid=Schdutydetail.getdutystaff (); +String noticeobj=Userbi.selectusernamebyid (userId); -             if(noticeobj!=NULL){ $String noticetype = msgconstant.msg_notice_type_msg;//Notification Type $String Noticemode = MSGCONSTANT.IMSG_NOTICE_MODE_SM;//notification method defaults to SMS -StringBuffer Dealinfostrbuff =NewStringBuffer (); -String time=dateformat.getinstance (). Format (Schdutydetail.getdutystarttime ()); theDealinfostrbuff.append (Noticeobj). Append ("Hello, you are in"). Append (Schdutydetail.getdutydetailid (). toString ()). Append (" The class table has ") -. Append (Time). Append ("The shift, please prepare in advance, the succession, thank you." The reminder comes from the integrated system. ");WuyiString noticecontent = dealinfostrbuff.tostring ();//Notification Content theString[] noticeargs={noticetype,noticemode,noticecontent, "",}; -Uservo uservo=Userbi.selectuserbyprimarykey (constant.default_user); WuMoncommonoperatebo.sendmessage (Schdutydetail.getdutytableid (), Schdutydetail.gettemplateid (), "0000017", Schdutydetail.getdutydetailid (). toString (), - noticeobj, Dealconstants.objecttype_baf_e, Uservo, Noticeargs);  About             }                        $         } -     } -}

How to optimize the two-part repetitive code in a class

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.