Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a memory and performance hit taken from using Bloch's Builder Pattern?

What is the memory and performance usage compared to creating a object with only a constructor?

The usage here is in creating a Set<Object> or List<Object> that may contain million plus entries and I am concerned with the overhead of using Bloch's Builder Pattern. I have used it in the past, but never in this large of a scope.

Reference: Item 2: Consider a builder when faced with many constructor parameters, reprinted in Creating and Destroying Java Objects: Part 1, excerpted from Effective Java Second Edition by Joshua Bloch.

like image 437
WolfmanDragon Avatar asked Mar 15 '10 16:03

WolfmanDragon


People also ask

What are the consequences of builder pattern?

Here are key consequences of the Builder pattern: It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product.

What are the advantages of builder design 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.

Is builder pattern thread safe?

Last, but not least, builders are not thread-safe. While immutable objects can be freely shared between threads, builders cannot because of the mutable state.

How does the builder pattern benefit a test case?

Using the Builder pattern to construct test fixtures Using a Builder allows test fixtures to be created more easily, and with clearer intent. The type of test objects I typically use this Builder approach for are domain model objects, such as Account, User, Widget or whatever.


1 Answers

You have the additional Builder-object, that is discarded after the creation of the object. So you may have some impact on memory-usage and speed. But the Java-VM does optimize very strongly, especially the Server-VM (java -server), so the VM may optimize away the builder completely. So my suggestion is you should measure the real impact (as always if you care about performance) and decide if the impact is too big.

like image 175
Mnementh Avatar answered Sep 20 '22 07:09

Mnementh