2 回答
TA贡献1887条经验 获得超5个赞
if (actualFileSize <= Long.parseLong(emailFileSizeLimit)) {
file = null;
}
return new Email(from, recipient, title,emailContent, null, file);
1)重构。如果您认为参数过多,您可以将该方法分成更小的、可管理的方法。
if (actualFileSize <= Long.parseLong(emailFileSizeLimit)) {
file = null;
}
createMail(from, recipient, title,emailContent, null, file);
******
private Email createMail(long actualFileSize, String from, String recipient, String
title, String emailContent, MultipartFile attachment, File file) {
return new Email(from, recipient, title,emailContent, null, file);
}
2) DTO。您在 dto 中提供参数并将其作为请求/响应传递到整个链中,在需要时使用您需要的元素。当您需要创建时,您可以添加更多属性。您通过 setter 提供它们并在需要时传递
3) 建造者。您在类中完成了在完成操作后返回自身的逻辑。您可以通过对同一类调用不同的操作并返回结果来执行下一个操作。
Mail mail = mailBuilder.addName(name).add(stuff).compile(); // will call constructor inside
4) 工厂
让工厂包含所有方法并让它决定调用什么方法。它可以是静态的或托管的
factory.createBasicMail(name);
factory.createFileMail(name, file);
ps:为了您的心理健康,请使用 Lombok
TA贡献1820条经验 获得超10个赞
这似乎很适合Builder 模式,在那里您需要以精心设计的方式构造一个新对象:
public class EmailBuilder {
private Email mail;
private String from;
private String recipient;
// other properties
public EmailBuilder withFrom(String from) {
this.from = from;
return this;
}
public EmailBuilder withRecipient(String recipient) {
this.recipient = recipient;
return this;
}
public Email send() {
mail = new Email(from, recipient, title,emailContent, null, file);
return mail;
}
}
用法
Email mail = new EmailBuilder()
.withFrom("John Doe")
.withRecipient("Mary Doe")
.with...
.send();
添加回答
举报