When would you use the Builder Pattern?

Charles Graham

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

JoshBerke

The key difference between a builder and factory IMHO, is that a builder is useful when you need to do lots of things to build an object. For example imagine a DOM. You have to create plenty of nodes and attributes to get your final object. A factory is used when the factory can easily create the entire object within one method call.

One example of using a builder is a building an XML document, I've used this model when building HTML fragments for example I might have a Builder for building a specific type of table and it might have the following methods (parameters are not shown):

BuildOrderHeaderRow()
BuildLineItemSubHeaderRow()
BuildOrderRow()
BuildLineItemSubRow()

This builder would then spit out the HTML for me. This is much easier to read than walking through a large procedural method.

Check out Builder Pattern on Wikipedia.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When and why would you use static with constexpr?

From Dev

When would you use a Mutex without an Arc?

From Dev

When would you use AffineTransform getScaleX()?

From Dev

When would you use reduce() instead of sum()?

From Dev

When would you use .concat() in javascript

From Dev

When would you use an additional file descriptor?

From Dev

Why would you use Builder.Source() in a custom computation expression builder?

From Dev

When would I want to use a Free Monad + Interpreter pattern?

From Dev

When would you use different counts or types for sending and receiving processes?

From Dev

When would you use an ES6 Map over an Object?

From Dev

When would you use a column name instead of * in a count?

From Dev

When would you use jqGrid no_legacy_api = false?

From Dev

In Powershell, what kind of data type is [string[]] and when would you use it?

From Dev

When would you use the optional argument thisArg for the filter prototype?

From Dev

When would you use jqGrid no_legacy_api = false?

From Dev

When would you use mysqli with ssl (ssl_set)

From Dev

When would you use ssh -L instead of ssh -D

From Dev

When would you use object BigInteger instead of simply using double?

From Dev

When would you use a Map over a WeakMap when you have objects as keys?

From Dev

When would you use a Map over a WeakMap when you have objects as keys?

From Dev

Design pattern to use when you need to initialize your object?

From Dev

Design pattern to use when you need to initialize your object?

From Dev

Why would you use `init!` instead of `init?` when implementing a Failable Initializer in Swift?

From Dev

ZF2: When would you use autoload_classmap.php

From Java

When would you use .git/info/exclude instead of .gitignore to exclude files?

From Dev

When a class is created in Python, and an object referencing it is used, how would you use the name of the object in the class?

From Dev

What field type would you use to show when a post is published (Rails)

From Dev

What does the default TArray.Sort comparator actually do and when would you use it?

From Dev

When would you use apt-get remove over apt-get autoremove?

Related Related

  1. 1

    When and why would you use static with constexpr?

  2. 2

    When would you use a Mutex without an Arc?

  3. 3

    When would you use AffineTransform getScaleX()?

  4. 4

    When would you use reduce() instead of sum()?

  5. 5

    When would you use .concat() in javascript

  6. 6

    When would you use an additional file descriptor?

  7. 7

    Why would you use Builder.Source() in a custom computation expression builder?

  8. 8

    When would I want to use a Free Monad + Interpreter pattern?

  9. 9

    When would you use different counts or types for sending and receiving processes?

  10. 10

    When would you use an ES6 Map over an Object?

  11. 11

    When would you use a column name instead of * in a count?

  12. 12

    When would you use jqGrid no_legacy_api = false?

  13. 13

    In Powershell, what kind of data type is [string[]] and when would you use it?

  14. 14

    When would you use the optional argument thisArg for the filter prototype?

  15. 15

    When would you use jqGrid no_legacy_api = false?

  16. 16

    When would you use mysqli with ssl (ssl_set)

  17. 17

    When would you use ssh -L instead of ssh -D

  18. 18

    When would you use object BigInteger instead of simply using double?

  19. 19

    When would you use a Map over a WeakMap when you have objects as keys?

  20. 20

    When would you use a Map over a WeakMap when you have objects as keys?

  21. 21

    Design pattern to use when you need to initialize your object?

  22. 22

    Design pattern to use when you need to initialize your object?

  23. 23

    Why would you use `init!` instead of `init?` when implementing a Failable Initializer in Swift?

  24. 24

    ZF2: When would you use autoload_classmap.php

  25. 25

    When would you use .git/info/exclude instead of .gitignore to exclude files?

  26. 26

    When a class is created in Python, and an object referencing it is used, how would you use the name of the object in the class?

  27. 27

    What field type would you use to show when a post is published (Rails)

  28. 28

    What does the default TArray.Sort comparator actually do and when would you use it?

  29. 29

    When would you use apt-get remove over apt-get autoremove?

HotTag

Archive