问题描述
我已经看到了一些API的设计的方式,你必须把它们作为低于code
I have seen that some API's are designed in the way that you must use them as below code
Class.doThis("...").doThat("...").....
例如HTTPCommon(流利API)可作为:
For example HTTPCommon (Fluent API) can be used as:
Request.Get("http://somehost/")
.connectTimeout(1000)
.socketTimeout(1000)
.execute().returnContent().asString();
石英时间表可以用作
The quartz-schedule can be used as:
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
的SimpleCatptch可以用作
The SimpleCatptch can be used as:
Captcha captcha = new Captcha.Builder(200, 50)
.addText()
.addBackground()
.addNoise()
.gimp()
.addBorder()
什么是这种API设计的名字吗?当它是很好的,设计也是这样吗?
What is the name of this kind of API design? When it is good to design like this?
推荐答案
它简单地称为流畅,作为HTTPCommon指出。这是常见的建设者有一个流畅的布局,但生成器模式是正交的:流利的API是约可读链式方法调用,而建设者们关于指定一个完整的配置对象,然后在一个完整的国家建设,一次就全部。
It's simply called "fluent", as HTTPCommon noted. It's common for builders to have a fluent layout, but the builder pattern is orthogonal: Fluent APIs are about readable chained method calls, while builders are about specifying a complete configuration for an object and then constructing it in a complete state all at once.
这风格是合适的,只要它使code可读;这是特别有帮助时,IDE自动完成可以帮助程序员。两种最常见的用例的配置(无论是制造商或弹簧式的configurers)和数据管道(如Java 8流和反应式编程)。
This style is appropriate whenever it makes the code readable; it's especially helpful when IDE autocompletion can assist the programmer. The two most common use cases are configurations (either builders or Spring-style configurers) and data pipelines (such as Java 8 streams and reactive programming).
这篇关于设计一个API和级联功能调用(Class.doThis(QUOT; ...")找时间做(QUOT; ...").....)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!