Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

What are some commonreal-world examples of using the Builder Pattern? What does it buy you? Why not just use a Factory Pattern?

1 Answer

0 votes
by (46k points)

The fundamental distinction between a builder and factory IMHO, is that a builder is helpful when you need to do loads of things to build an object. For example, assume a DOM. You have to perform plenty of nodes and attributes to get your last object. A factory is used when the factory can simply create the entire object inside one method call.

One example of practicing a builder is a building an XML document, I've handled this model when building HTML fragments, for example, I might have a Builder for building a distinct type of table and it might have the subsequent methods (parameters are not displayed):

BuildOrderHeaderRow()

BuildLineItemSubHeaderRow()

BuildOrderRow()

BuildLineItemSubRow()

This builder would then eject out the HTML for me. This is much more comfortable to read than driving through a big procedural method.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 13, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...