Nested Builders

a simple solution for complex cases

Consider the following EmailMessage Builder client code:

Now it is requested to allow to send the email to more than one email address and also to allow to personalize the content of the email.

No problem, it can be solved using nested Builder classes, that is, apply the Builder pattern for EmailContent and EmailRecipient.

So we can refactor the previously solution to something like this.

Lets get to work!

  1. Lets started creating EmailContent class using the Builder creational design pattern.

2. Create the EmailRecipient class using the Builder creational design pattern.

3. Finally change the EmailMessage class to use EmailContent and EmailRecipient classes. See all these changes below.

What’s next

Start learning about Developer-Friendly builders: https://lucasferreiramachado.medium.com/developer-friendly-builders-a098518feb1a

--

--