lundi 3 avril 2017

how to combine actions by different conditions

now i have a notice service,with sms,voice,app push (all are interface) etc to notify someone.

public class NoticeService{
    SmsService smsService;
    VoiceService voiceService;
    AppPushingService appPushingService;

    sendSms(){ smsService.send(); };
    sendVoice(){ voiceService.send(); };
    sendAppPushing(){ appPushingService.send() };

}

if i want to send two kinds of notice,i should add a new method like:

public class NoticeService{
    SmsService smsService;
    VoiceService voiceService;
    AppPushingService appPushingService;

    sendSms(){ smsService.send(); };
    sendVoice(){ voiceService.send(); };
    sendAppPushing(){ appPushingService.send() };

    sendSmsAndVoice(){
       sendsms();
       sendVoice;
   }
   sendSmsAndAppPushing(){
       sendSms();
       sendAppPushing();
   }
}

it's so messy.i'd like to combine these with different conditions.and i don't want to use if or switch to decide the action, i want to have a method auto decided by the statement like this

sendNotice(Condition smsAndVoice){      
     //when the statement changed,the action also change.
     smsService.send();
     voiceService.send(); 
}

if you also have some hints here,please help me,thank you!

Aucun commentaire:

Enregistrer un commentaire