Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real-world examples of the Builder pattern

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

Could you please tell me about some usages of this patten in real-world applications/APIs, preferably from the world of C++, .NET or PHP (as those are the languages I'm familiar with).

Thanks.

like image 408
Matěj Zábský Avatar asked Mar 06 '11 14:03

Matěj Zábský


People also ask

What is pattern discuss the Builder design pattern with example?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.

Why would one use the builder pattern?

Advantages of the Builder pattern include: Allows you to vary a product's internal representation. Encapsulates code for construction and representation. Provides control over steps of construction process.

What is builder pattern in Java with example?

Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.

Which is an example of a creational design pattern?

The Singleton Design Pattern is a Creational pattern, whose objective is to create only one instance of a class and to provide only one global access point to that object.


2 Answers

Update: I recently came across an even better example (imo). Checkout the JobBuilder and TriggerBuilder implementations in the Quartz scheduler package: http://quartz-scheduler.org/api/2.1.5/

Also, when I have time, just for fun/practice, I try to write examples of all GoF patterns in java. Just recently, I used the Builder pattern to make it easy to generate different types of Sitemaps (google site map vs, html site map, etc). The code is in java, but you might be useful: https://github.com/dparoulek/java-koans/tree/master/src/main/java/com/upgradingdave/koans/builder

Good question, I'd be interested in seeing more modern examples too.

like image 184
Upgradingdave Avatar answered Nov 09 '22 05:11

Upgradingdave


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();
like image 28
Anoop Avatar answered Nov 09 '22 06:11

Anoop