The builder design pattern is one of the most useful creational patterns as it builds larger objects from smaller ones. This is precisely what we want to do to construct a sandwich object from a list of ingredients. The builder pattern has a further advantage in that optional features are easy to include later. As before, we will begin by creating an interface; we will call it
Ingredient and use it to represent both
filling. This time, we will need to represent calories as an integer, as we will need to calculate the total amount in a finished sandwich.
Open an Android Studio project, or start a new one, and follow the proceeding steps to create a basic sandwich builder pattern: