Real-world examples of the Builder pattern

后端 未结 3 1877
孤独总比滥情好
孤独总比滥情好 2021-02-04 06:21

I would like to see how is Builder pattern used in real world applications/APIs. The examples I found are all pizzas, cakes, cars et cetera (plus the parser example from the GoF

3条回答
  •  既然无缘
    2021-02-04 06:43

    Builder pattern is used in javax.json.Json and javax.json.JsonBuilder classes while building Json objects.

    Good explanation is at http://www.programcreek.com/java-api-examples/index.php?api=javax.json.JsonObjectBuilder and also check out its official documentation.

    JsonObjectBuilder b = Json.createObjectBuilder().
                add( "report", Json.createObjectBuilder().
                     add( "reportId", reportId ).
                     add( "title", title ).
                     add( "subtitle", subTitle == null ? "" : subTitle ).
                     add( "created", created.toString() ).
                     add( "description", description == null ? "" : description ).
                     add( "data", report )
                );
    return b.build();
    

提交回复
热议问题