Use Builders... Cautiously - Effective Java, Item 2
Channel: nipafx
Date: 2018-10-09

The builder pattern is a powerful tool to ease the instantiation of complex classes. Whether constructor parameters are too numerous, there are too many of the same type, or whether many are optional - with a builder you can make your life easier. Although, I posit, often you can make your life even easier by directly tackling the class' or constructor's complexity.

In this video I show an example of how to simplify a class to make a builder obsolete, but also how to build more powerful builders that add more value than just simplifying constructor calls.

Table of contents:

00:00 Mixed feelings
02:42 Builders explained
03:55 └ A better solution to Josh's example
06:20 └ An even better solution to Josh's example
08:26 Random details (threading, style, other languages)
10:50 Builders as DSL
13:43 More powerful builders
15:31 Inheriting builders and recursive generics 
20:14 Refactor towards builder
22:56 Summary

Repository with examples: https://github.com/CodeFX-org/effective-java
JavaZone in Oslo: https://2018.javazone.no/

My opinion on Optional: https://blog.codefx.org/techniques/intention-revealing-code-java-8-optional/
Map.of et al: https://blog.codefx.org/java/java-9-tutorial/#Collection-Factories
Named & default parameters in Kotlin: http://www.deadcoderising.com/kotlin-how-to-use-default-parameters-in-functions-and-constructors/
Automaton: https://brilliant.org/wiki/finite-state-machines/
Partial application: https://en.wikipedia.org/wiki/Partial_application
Self types with generics: https://www.sitepoint.com/self-types-with-javas-generics/

Nicolai:
    * home: http://codefx.org
    * online Java courses: http://courses.codefx.org
    * on Twitter: https://twitter.com/nipafx

Affiliate links for Effective Java, Third Edition:
    * InformIT (the publisher): http://tiny.cc/effective-java-3rd
    * Amazon USA: https://amzn.to/2QI0D0S
    * Amazon Germany: https://amzn.to/2OvsWOu