October 2018
Intermediate to advanced
370 pages
9h 15m
English
The Builder pattern is useful when we need to initialize many fields during new object creation. According to this pattern, instead of using a constructor with a lot of parameters, we can create a nested class that collects all passed arguments and constructs a new object. Let's look at the typical implementation of this pattern in a classic Java way. Let's imagine that we need to cook a burger. For this, we need to define classes of ingredients:
class Meatclass Cheeseclass Ketchupclass Bun
We also need to define the Burger class, which encapsulates all ingredients:
class Burger { private val meat: Meat private val cheese: Cheese private val ketchup: Ketchup private val topBun: Bun private val bottomBun: Bun private constructor(meat ...
Read now
Unlock full access