builder pattern vs constructor

21
Builder Pattern vs Constructor Liviu Tudor Netflix Ads Engineering CODE PRACTICES DISCUSSION

Upload: liviu-tudor

Post on 13-Apr-2017

104 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Builder pattern vs constructor

Builder Pattern vs Constructor

Liviu TudorNetflix Ads EngineeringCODE PRACTICES DISCUSSION

Page 2: Builder pattern vs constructor

Contents.● What?● Why?● When would you use it?● Pros and Cons● Conclusion

Page 6: Builder pattern vs constructor

● Inspired by the Software Craftsmanship Manifesto:

● Establish guidelines which leads to elegant and robust code

Why?

Not only working software,but also well-crafted software

Page 7: Builder pattern vs constructor

● NO !!!

Does it matter?

Page 8: Builder pattern vs constructor

● NO … unless we are talking about creating immutable objects

Does it matter?

Page 9: Builder pattern vs constructor

● NO … unless we are talking about creating immutable objects● Why?

● We can set any property on the beanwhenever we want

Does it matter?

Page 10: Builder pattern vs constructor

● Things change once we make objects immutable via “final” keyword*

● Member data can only be set once, so it has to be ALL available at the time we create the object

* Not the only way to make objects immutable

Does it matter?

Page 11: Builder pattern vs constructor

Only relevant for immutable objects. For the rest just use a regular Java bean.

Page 12: Builder pattern vs constructor

● The idea: pass all parameters in one call to constructor to build object

● Pros: very concise code (most IDE’s have generators)

-- for both the bean itself and the creation Fast code

Constructor.

Page 13: Builder pattern vs constructor

● The idea: pass all parameters in one call to constructor to build object

● Cons: All data has to be available at creation time

-- sometimes this is not possible as some data might notbe available (and we need to run code to check)

Constructor.

Page 14: Builder pattern vs constructor

● The idea: pass all parameters in one call to constructor to build object

● When do use it: When we have all the data upfront Or we can compute it

Constructor.

Page 15: Builder pattern vs constructor

Constructors provide concise code and can be used when all data is available.

Page 16: Builder pattern vs constructor

● The idea: pass only the available data one by one when available to a builder which then constructs object

Builder.

Page 17: Builder pattern vs constructor

● The idea: pass only the available data one by one when available to a builder which then constructs object

● Pros: We only set the data we have available

-- so if we don’t have data for some propertieswe don’t call the withXyz() method

Also we can compute the data one at a time and set it as the result becomes available

Builder.

Page 18: Builder pattern vs constructor

● The idea: pass only the available data one by one when available to a builder which then constructs object

● Cons: Verbose code Extra memory – we create a builder object

first which stores all the data then create our final objects which also copies the data

Slow – the generated code: Creates builder object Calls methods on it to set data Calls method on it to create our object Creates our object and copies the data

Builder.

Page 19: Builder pattern vs constructor

● The idea: pass only the available data one by one when available to a builder which then constructs object

● When to use it: When we don’t have all data available Or we need to compute it one by one (e.g. asynchronous

calls)

Builder.

Page 20: Builder pattern vs constructor

Builders allow building objects when we don’t have all the members data available but we trade off some speed and code length for that.

Page 21: Builder pattern vs constructor

QuestionsDiscussion