builder pattern vs constructor
TRANSCRIPT
Builder Pattern vs Constructor
Liviu TudorNetflix Ads EngineeringCODE PRACTICES DISCUSSION
Contents.● What?● Why?● When would you use it?● Pros and Cons● Conclusion
● Comparing “Constructor” with “Builder Pattern”
What?
● Comparing “Constructor” with “Builder Pattern”
What?
● Comparing “Constructor” with “Builder Pattern”
What?
Fluent Interface
● 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
● NO !!!
Does it matter?
● NO … unless we are talking about creating immutable objects
Does it matter?
● NO … unless we are talking about creating immutable objects● Why?
● We can set any property on the beanwhenever we want
Does it matter?
● 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?
Only relevant for immutable objects. For the rest just use a regular Java bean.
● 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.
● 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.
● 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.
Constructors provide concise code and can be used when all data is available.
● The idea: pass only the available data one by one when available to a builder which then constructs object
Builder.
● 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.
● 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.
● 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.
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.
QuestionsDiscussion