【发布时间】:2015-12-22 19:42:05
【问题描述】:
我正在处理遗留代码,其中一个方法应该基于多个参数返回结果。代码中使用的模式有效,但我正在考虑是否可以进一步改进。我举个例子
假设我有一个具有各种属性的类 JobPosting
class JobPosting {
int jobPostingId;
String description;
Dept dept; // A posting ca belong to 1 department at a time
Location loc; // A job posting can belong to 1 location at a time
String created By;
int headcount; // number of people required to fill the job posting
List<User> assignedUsers; // number of people who have applied for the posting already
List<User> reservedUsers; // users who are eligible to apply for the posting, if this is empty, then anyone can apply to posting
DateTime visibleDate; // date from when posting is visible to users
Date endDate; // posting's deadline
...
}
这些过帐的详细信息存储在数据库中的一个表中
现在我有各种用例来获取帖子:
- Get job postings for a loc & dept- I only care about description and headcount.
- Get job posintgs for a loc & dept with users who have filled the postings
- Get job postings for a loc & dept with users who have filled and users who are eligible for the postings
- Get job postings for a loc & dept which are visibleToUsers (visibleDate < curdate)
等等,我可以根据场景有多种获取帖子的可能性。
目前的实现是我们有参数的构建器模式
class postingsParams {
boolean excludeFilledPostings;
boolean excludeNonVisiblePostings;
boolean returnAssignedUsers;
boolean returnEligibleUsers;
}
还有一种方法,它接受参数并确定要填写 JobPostings 中的哪些字段以及要排除哪些帖子
getPostingsByLocDept(loc, dept, postingsParamas) { ...// code calls dynamic SQL queries to return results and if conditions to filter }
这似乎工作正常,但我不喜欢的是
- Each caller must be aware of all the possible crtireia to search
- If in future I want to add another criteria like excludePostingsWithPassedDeadline, I need to make changes to this method and need to test all the places where it is called to make sure nothing breaks
一种方法是为用例创建特定的 get 方法,但使用这种方法我们最终可能会得到多个 get 方法,这可能是不可取的。
有没有办法改进这种设计,或者当前的方法是处理这种情况的最佳方法吗?
【问题讨论】:
-
我建议这个问题更适合codereview.stackexchange.com,前提是添加了一些工作代码。
标签: java design-patterns