我的练习非常简单,但是由于我的知识和使用设计模式(+单元测试)的要求,我受到了限制。一个项目的目标是创建一个控制台应用程序,该应用程序将允许您从集合中保存(添加),打印(显示所有),删除(按标准删除)和过滤(按标准显示)消息。
private String title;
private String author;
private String content;
private String creationDate;
我能够创建“添加”功能并“显示全部”。我的问题是过滤。我必须创建一个选项,以根据用户给出的条件过滤保存的对象(所有可能的组合,例如:按标题过滤和creationDate,按标题过滤等)。我考虑过要给用户一个选项,可以使用以下开关和方法从菜单中选择它:
private final List<Message> storage = new ArrayList<Message>();
public List<Message> getAll() {
final ArrayList<Message> messages = new ArrayList<>();
messages.addAll(storage);
return messages;
}
List<Message> find(String author) {
return simpleStorage.getAll().stream()
.filter(item -> item.getAuthor() == author)
.collect(toList());
}
但是我认为复制很多类似的代码不是一个好习惯。另外,我将来可能会发现自己的解决方案将很累,甚至是不可能的(每个新参数都会添加新组合)。有更好的方法吗?就像选择“一个一个”的标准,以便用户可以自己创建一个组合?我有一个提示,谓词可以帮助我解决这个问题,但是我不知道该怎么做。
答案 0 :(得分:1)
对于每个条件,您可以有一个BiPredicate<String, Message>
1 ,它从用户那里获取原始输入和一条消息,并告诉消息该消息是否与过滤选项 2 < / sup>。
Map<String, BiPredicate<String, Message>> criteria = Map.of(
"title", (userTitle, message) -> input.equals(message.getTitle())
...
);
我将为您提供该地图的简化示例:
Scanner scanner = new Scanner(System.in);
String filteringOption = scanner.nextLine();
String userInput = scanner.nextLine();
BiPredicate<String, Message> predicate = criteria.get(filteringOption);
// get all messages from the storage
getAll()
// make a stream out of them
.stream()
// apply the filtering rule from the map
.filter(m -> predicate.test(userInput, m))
// collect them into a list to display
.collect(Collectors.toList());
后来,这些谓词可以通过or()
,and()
之类的逻辑运算进行组合以形成自定义过滤器选项。可以将用户的选项添加到地图中以进行随后的呼叫,也可以在用户每次请求时即时进行计算,例如
BiPredicate<String, Message> titleAndDateFilter =
criteria.get("title").and(criteria.get("date"));
1 您可以使用Predicate<Message>
,但是由于需要将消息的上下文与给定的输入进行比较,这会使这些功能的隔离度降低。 < br />
2 我使用了Java 9的Map.of
。
答案 1 :(得分:1)
我的问题是过滤。我必须创建一个过滤条件 根据用户指定的条件保存对象(所有可能 组合,例如:按标题和creationDate过滤,按标题过滤 等)。
这是您可以尝试,使用或即兴使用的东西。该代码是一个有效的示例(使用Java SE 8)。该示例具有MessageFilter
类:
List
条测试消息。Predicate
:
getPredicate
方法。该示例分别显示按“标题”和“作者”进行过滤。我认为示例中的概念可以应用于其他过滤条件。
示例代码:
class Message { // represents a message
private String title;
private String author;
Message(String s1, String s2) {
title = s1;
author = s2;
}
String getTitle() {
return title;
}
String getAuthor() {
return author;
}
public String toString() {
return String.join(", ", "("+title, author+ ")");
}
}
public class MessageFilter {
public static void main(String [] args) {
// Create some messages
Message [] array = {new Message("msg1", "auth1"),
new Message("msg2", "auth2"),
new Message("msg3", "auth1"),
new Message("msg9", "auth3")
};
List<Message> messages = Arrays.asList(array);
System.out.println(messages);
// Accept user input: the field name and its value
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the property to filter (title, author, etc): ");
String filterCriteria = scanner.nextLine();
System.out.print("Enter the property value: ");
String filterValue = scanner.nextLine();
// Get the predicate based on user input
Predicate<Message> predicate = getPredicate(filterCriteria, filterValue);
// Filter the data using the predicate got from user input, and print...
List<Message> result = messages.stream()
.filter(predicate)
.collect(Collectors.toList());
System.out.println("Result: " + result);
}
private static Predicate<Message> getPredicate(String criteria, String value) {
Predicate<Message> p = msg -> true; // by default returns all messages
switch(criteria) {
case "title":
p = msg -> msg.getTitle().equals(value);
break;
case "author":
p = msg -> msg.getAuthor().equals(value);
break;
}
return p;
}
}