最近,我一直在使用谓词和番石榴工具。 我创建了一个Utils.class,用于存储我在代码的不同部分使用的谓词。 因此,这个问题已经出现,我们(我和我的同事)没有就此达成一致。
将谓词放入实用类中的正确方法或“良好实践方式”是什么,常常用大写字母或静态方法来定义它。 以下我写一个例子:
public final class Utils { public static final Predicate<Element> IS_SPECIAL = new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; } }; public static Predicate<Element> isSpecial() { return new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; }}顺便说一句,番石榴提供了一些预判谓词,它提供了它作为返回谓词的方法,但其他libreries也提供它们作为常量。
Lately, I have been working with predicates and guava utilities. I have created a Utils.class where I store some predicates that I use in different parts of the code. Thus this issue has come up and we (me and my workmates) do not get an agreement about it.
What is the right way or "good practice way" to put a predicate in a utility class?, as a constant defining it with capital letter or in a static method?. Following, I write an example:
public final class Utils { public static final Predicate<Element> IS_SPECIAL = new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; } }; public static Predicate<Element> isSpecial() { return new Predicate<Element>() { @Override public boolean apply(Element elem) { return elem.special; }}By the way, guava offers some predifining predicates and it provides them as a method that returns predicates, but other libreries do the same offering them as constants.
更多推荐
发布评论