API设计:一个通用接口VS三个专用接口?
我正在开发一个工具,用户可以使用自己的注释来描述数据处理工作流程(如验证、转换等)。
除了使用现成的注释之外,用户还可以使用自己的注释:为此,他们需要声明注释类本身,然后实现注释处理器(<--这实际上是这个问题的要点)。
配置的数据处理方法可能如下所示:
void foo(@Provide("dataId") @Validate(Validator.class) String str) {
doSmth(str);
}
自然存在三组注释:产生初始值的注释;
- 产生初始值的注释;产生初始值的注释。
- 那些转换值的(转换器);
- 那些只是读取值并执行一些工作的人(验证者,不同的消费者)。
所以我需要做出选择:要么创建一个接口来处理所有这些类型的注释,它看起来像这样:
interface GenericAnnotationProcessor {
Object processAnnotation(Annotation annotation, Object processedValue);
}
或者我可以向 API 添加 3 个接口:
interface ProducerAnnotationProcessor {
Object produceInitValue(Annotation annotation);
}
interface TransformerAnnotationProcessor {
Object transformValue(Annotation annotation, Object currentValue);
}
interface ConsumerAnnotationProcessor {
void consumeValue(Annotation annotation, Object currentValue);
}
第一个选项在使用中不是很清楚,但第三个选项选项用 3 个几乎相似的接口污染了 API。
您会选择什么(首先作为 API 用户)?为什么?
谢谢!
I'm working on a tool where users can use their own annotations to describe data processing workflow (like validation, transformation etc).
Besides using ready-to-use annotations, users can user their own: in order to do this they need to declare annotation class itself, and then implement annotation processor (<--it's the main point of this question actualy).
The configured method for data processing may look like this one:
void foo(@Provide("dataId") @Validate(Validator.class) String str) {
doSmth(str);
}
There're naturally three groups of annotations:
- those which produce initial values;
- those which transforms values (converters);
- those which just read values and perform some work (validators, different consumers).
So I need to make a choise: either create one interface for handling all these types of annotations, which can look like this one:
interface GenericAnnotationProcessor {
Object processAnnotation(Annotation annotation, Object processedValue);
}
Or I can add 3 intefaces to the API:
interface ProducerAnnotationProcessor {
Object produceInitValue(Annotation annotation);
}
interface TransformerAnnotationProcessor {
Object transformValue(Annotation annotation, Object currentValue);
}
interface ConsumerAnnotationProcessor {
void consumeValue(Annotation annotation, Object currentValue);
}
The first option is not very clear in use, but the third option pollutes the API with 3 almost similar interfaces.
What would you choose (first of all as an API user) and why?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将创建第一个更通用的接口,然后定义三个不同的实现类。在不了解如何使用它的更多信息的情况下,我的第一直觉是定义接口和/或基类(取决于不同处理器之间共享的公共实现代码的数量),然后在派生中添加专用处理器实现类型,所有这些都共享公共接口。
在使用 API 时,我希望声明一个实现 GenericAnnotationProcessor 的变量,然后根据我的需要分配适当的实现类型。
在俄勒冈州波特兰,现在还很早,但此时此刻,我所需的咖啡因含量为 50%,在我看来,这将提供最大的灵活性,同时最大限度地提高 cade 的重复利用。
当然,您的实际需求可能会有所不同。 。 。
希望这有帮助!
I would create the first, more general interface, then define the three different implementation classes. Without knowing more about how you will be using this, my first instinct would be to define the Interface and/or a base class (depending upon how much common implementation code was shared between the different processors), and then add specialized processor implementation in derived types, all of whihc share the common interface.
In using the API, I would expect to declare a variable which implements GenericAnnotationProcessor, and then assign the appropriate implementation type depending upon my needs.
It is early here in Portland, OR, but at this moment, at 50% of my required caffeine level, this seems to me like it would provide maximum flexibility while maximizing cade re-use.
Of course, your actual reuirements might dictate otherwise . . .
Hope that was helpful!
只是深入研究你的问题。
由于他们正在执行类似的任务,但存在一些差异,策略模式#Example应该可以帮助您。
您的问题应该如下所示。
现在你可以按照Wiki上的例子
相信你能理解。
Just diving deep into your problem.
As they are executing similar task, with some variance, Strategy pattern #Example should assist you.
Your problem should look like something below.
Now you can follow example from Wiki
I believe you can understand.
您可以使用扩展接口的接口 更多信息
类似对于类,可以使用extends关键字构建接口的继承层次结构,如下所示:
在本例中,interface Soakable扩展了interface Washable。因此,Soakable 继承了 Washable 的所有成员。实现 Soakable 的类必须为 Soakable 中声明或继承的所有方法(wash() 和 soak())提供主体,或者声明为抽象方法。请注意,只有接口才能“扩展”其他接口。类不能扩展接口,只能实现接口。
希望有帮助。
You can use Interfaces Extending Interfaces More on there
Similar to classes, you can build up inheritance hierarchies of interfaces by using the extends keyword, as in:
In this example, interface Soakable extends interface Washable. Consequently, Soakable inherits all the members of Washable. A class that implements Soakable must provide bodies for all the methods declared in or inherited by Soakable, wash() and soak(), or be declared abstract. Note that only interfaces can "extend" other interfaces. Classes can't extend interfaces, they can only implement interfaces.
Hope it helps.