最近,我一直在使用谓词和番石榴实用程序.我创建了一个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;
}}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,番石榴提供了一些预测谓词,它提供了一个返回谓词的方法,但其他的libreries也提供它们作为常量.