vendredi 5 février 2016

Design an API with cascade function calls ( Class.doThis("...").doThat("...")..... )

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?

Aucun commentaire:

Enregistrer un commentaire