2

I have seen that some API's are designed in the way that you must use them as below code

Class.doThis("...").doThat("...").....

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();

The SimpleCatptch can be used as:

Captcha captcha = new Captcha.Builder(200, 50)
                  .addText()
                  .addBackground()
                  .addNoise()
                  .gimp()
                  .addBorder()

What is the name of this kind of API design? When it is good to design like this?

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173

3 Answers3

6

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.

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).

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
4

Your first example can be called method chaining.
Other examples have builder pattern, method chaining and fluent interface.

For a second part of your question see What is the difference between a fluent interface and the Builder pattern? and linked questions.

Community
  • 1
  • 1
Serikov
  • 1,159
  • 8
  • 15
0

It's just a builder pattern. From oodesign:

Motivation

The more complex an application is the complexity of classes and objects used increases. Complex objects are made of parts produced by other objects that need special care when being built. An application might need a mechanism for building complex objects that is independent from the ones that make up the object. If this is the problem you are being confronted with, you might want to try using the Builder (or Adaptive Builder) design pattern.

This pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the object's representation. This way the construction process can be used to create different representations. The logic of this process is isolated form the actual steps used in creating the complex object, so the process can be used again to create a different object form the same set of simple objects as the first one.

Basically, you can use it if the build process of your object is somewhat complex and want to expose a declarative way to build it.

Logain
  • 4,259
  • 1
  • 23
  • 32