简体   繁体   English

如何使用Temple方法,泛型方法或其他方法优化代码?现在我必须编写三遍类似的代码

[英]How to optimize my code using temple method or generic method or other method?Now I have to write the similar code three times

Now I want to send the message to the people which may be student,teacher or schoolmaster,and all of them have the phone,name and availdSendMsgCount.Before send the message which means insert the data to database,I have to judge whether them can be sent or not.So I write the code like this: 现在,我想将消息发送给可能是学生,老师或校长的人,他们所有人都有电话,姓名和availdSendMsgCount。在发送消息之前,这意味着将数据插入数据库,我必须判断他们是否可以无论是否发送,所以我写这样的代码:

if(sendType=SendType.student){
 List<Student> students=tStudentServiceImpl.queryByParam(conditionMap);
         sendPhoneCout=students.size(); 
         checkSendNumber(availdSendMsgCount,sendPhoneCout); 
Log log=logMsgSend(content,sendPhoneCout,sendType); 
         for (Student vo : students)      
 sendMsgItem(content,vo.getPhone(),vo.getName(),vo.getId(),log.getId()); 
}
 else if(sendType=SendType.teacher){
 List<Teacher> teachers=tTeacherServiceImpl.queryByParam(conditionMap);
         sendPhoneCout=teachers.size(); 
         checkSendNumber(availdSendMsgCount,sendPhoneCout); 
     Log log=logMsgSend(content,sendPhoneCout,sendType); 
         for (Teacher vo : teachers)      
   sendMsgItem(content,vo.getPhone(),vo.getName(),vo.getId(),log.getId()); 
}
else{
  List<Schoolmaster>  schoolmasters=tSchoolmasterServiceImpl.queryByParam(conditionMap);
         sendPhoneCout=schoolmasters.size(); 
         checkSendNumber(availdSendMsgCount,sendPhoneCout); 
     Log log=logMsgSend(content,sendPhoneCout,sendType); 
         for (Schoolmaster vo : schoolmasters)      
   sendMsgItem(content,vo.getMasterPhone(),vo.getMasterName(),vo.getId(),log.getId()); 
 }

I think this code is bad.But how to optimize? 我认为这段代码不好,但是如何优化呢?

When Schoolmaster, Teacher and Student implement the same interface or superclass then you could write one method with List as parameter which is doing all the stuff and that is called in each of your three cases. 当校长,老师和学生实现相同的接口或超类时,则可以编写一个使用List作为参数的方法,该方法可以完成所有工作,并且在三种情况下都将被调用。

On the other hand, why do you have three different classes? 另一方面,为什么会有三个不同的班级? They seem to have exactly the same methods. 他们似乎有完全相同的方法。 Wouldn't one be enough with a type parameter that is deciding what kind of type it is? 一个类型参数决定它是哪种类型还不够吗?

I suggest you to create an interface and implement it in your dao classes, so each of them will behave differently. 我建议您创建一个接口并在您的dao类中实现它,这样每个接口的行为都会有所不同。

public interface NameMe {
      void sendMsgItem(content, Integer logId); // it's not clear the type of content
}

There is the method 有方法

// replace T with type of 'conditionMap' object, it's not clear from your code
public static void getAndSendMessageItems(Function<T, List<NameMe>> serviceFunc,
        T conditionMap, Integer availdSendMsgCount, SendType sendType) { 
    List<NameMe> values = serviceFunc.apply(conditionMap);
    Integer sendPhoneCout = values.size(); 
    checkSendNumber(availdSendMsgCount, sendPhoneCout); 
    Log log = logMsgSend(content, sendPhoneCout, sendType); 
    for (NameMe i : values) {     
         i.sendMsgItem(content, log.getId());            
    }
}

And call it in your if/else 并在您的if / else中调用它

Function<T, List<NameMe>> serviceFunc = null; 
if (SendType.student.equals(sendType)) {
    serviceFunc = tStudentServiceImpl::queryByParam;
} else if (SendType.teacher.equals(sendType)) {
    serviceFunc = tTeacherServiceImpl::queryByParam;
} else {
    serviceFunc = tSchoolmasterServiceImpl::queryByParam;
}
getAndSendMessageItems(serviceFunc, conditionMap, availdSendMsgCount, sendType);

However, if your services implement the same interface, you even don't need to use Function<T, List<NameMe>> , you can just pass service via interface object. 但是,如果您的服务实现相同的接口,则甚至不需要使用Function<T, List<NameMe>> ,您只需通过接口对象传递服务即可。

Hope it helps! 希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我一直在编写此代码,并调用了方法replaceLessThan(x,e),但我无法正确编写此方法 - I have been working on this code and I called my method replaceLessThan(x,e), and I can't properly write this method 避免使用泛型方法在java中重复代码 - Avoid repetition of code in java using generic method 如何在我的方法中正确使用这个 while 循环,让用户重试输入最多 3 次 - How to properly use this while loop within my method to have user retry input to a maximum of three times Java:如何编写通用方法? - Java: How do I write a generic method? 我得到了我的 insertSorted 方法来对随机整数进行排序,但是现在我的代码没有按预期迭代 25 次,我做错了什么? - I got my insertSorted method to sort the random integers, but now my code isn't iterating 25 times as it should, what am I doing wrong? 如何在我的 Java 代码中将调用或方法编写为正则表达式? - How to write a call or method as a regular expression in my Java code? 如何使用 main 方法编写 java 类以转发到我的 Scala 代码? - How to write a java class with main method to forward to my scala code? 如何使用 URL 或任何其他方法从亚马逊读取 html 代码? 503代码出现 - How can I read an html code from Amazon by using URL or any other method ? 503 Code Appears 如何将onClickLIstener添加到我的代码中以执行已经实现的方法 - How do I add an onClickLIstener to my code to carry out a method I already have implemented 我如何编写一种方法来使此代码可重用? - How can I write a method to make this code reuseable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM