java - Design an API with cascade function calls ( Class.doThis("...").doThat("...")..... ) -
i have seen api's designed in way must use them below code
class.dothis("...").dothat("...").....
for example httpcommon (fluent api) can used as:
request.get("http://somehost/") .connecttimeout(1000) .sockettimeout(1000) .execute().returncontent().asstring();
the quartz-schedule can used as:
jobdetail job = newjob(hellojob.class) .withidentity("job1", "group1") .build();
the simplecatptch can used as:
captcha captcha = new captcha.builder(200, 50) .addtext() .addbackground() .addnoise() .gimp() .addborder()
what name of kind of api design? when design this?
it's called "fluent", httpcommon noted. it's common builders have fluent layout, builder pattern orthogonal: fluent apis readable chained method calls, while builders specifying complete configuration object , constructing in complete state @ once.
this style appropriate whenever makes code readable; it's helpful when ide autocompletion can assist programmer. 2 common use cases configurations (either builders or spring-style configurers) , data pipelines (such java 8 streams , reactive programming).
Comments
Post a Comment