In short: using the Builder Pattern is a good idea when using the factory pattern would result in factory methods with many parameters that are hard to remember and maintain.
Consider the Person class with a required name and an optional email address:
Person
- name
If you choose to use Factory pattern you will end up writing the following constructors:
After a while you realize you will need a new attribute: phone number. Your code or other's code already started using the constructors, so refactoring would be expensive, so you want to stay backward compatible. It leaves you no other choice, but overloading your constructor:
With each new attribute you will end up adding a new constructor unless you can refactor the code calling this class. After adding more than a handful of parameters the code will become less readable and it will be harder to remember the order of parameters and the constructor to be called.
Now, consider the following Builder implementation:
With using Builder pattern you can do the below initially:
After adding the phone attribute, the calling code still stay readable: