@FunctionalInterface的命名约定

3
按照惯例,类型应该用名词命名,这没问题。然而,@FunctionalInterface 是特殊类型 - 行为类似于函数/方法的对象类型。在函数方面,我觉得选择一个动词比选择一个名词更自然,例如 decorate.apply(o) 而不是名词 decorator.apply(o)。还有一些表示活动的名词,例如 decoration.apply(o)
如果我看一下java.util.function 包中所有的函数都是用名词命名的: Function, Operator, Consumer, Supplier,... 我应该优先选择名词动词表示动词的名词更新 有时候,我们可以通过返回一个对象/函数的工厂方法来获得一些帮助。
// toList factory returning Collector instance
list.stream().filter(...).collect(toList()); 

类名是名词,方法和函数名是动词-如果您在规范中看到某个名词名称,那意味着某个地方有一个具有该名称的类或接口。每当您有一个对象时,它由名词来标识,每当您有一个行为时,它由动词来标识。 - undefined
1个回答

3

@FunctionalInterface 只能标记类型(类、接口、注解、枚举)。

命名类型时应使用名词,而对于方法,则需要使用动词。

Classes

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

class Raster; 
class ImageSprite;

Methods

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

run(); 
runFast(); 
getBackground();

Naming Conventions, Oracle

这是一个关于如何定义函数式接口的示例:

以下是我定义函数式接口的示例。

@FunctionalInterface
interface Decorator {

    void decorate();

}

在代码中,它看起来可读性很高。

Decorator decorator = () -> {};
decorator.decorate();

我对“装饰器 vs 装饰品”的看法:

"装饰品"更多地描述了过程/结果,而"装饰器"是启动该过程/获得该结果的机制/工具/手段。

如果我在代码中看到Decoration类,我会想到一堆装饰品(如装饰品、油漆、壁纸),而不是真正的装饰者。

如果我看到DecoratorPainter,我会期望像decorate()paint()这样的方法,因为这是他们可以/应该做的。

比较suppliance/supplier -> get()consumption/consumer -> accept(t)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接