A sandwich builder pattern
The builder pattern is purpose-built for combining simple objects to form one complex object, and this forms a perfect analogy of making a sandwich. We encountered a generalized builder pattern earlier in the book, but now we need to adapt it for a specific function. Furthermore, we will be connecting the pattern to a working UI so that a sandwich can be constructed according to user selections rather than the set meal demonstrated in previous builder examples.
Applying the pattern
To keep the code short and simple, we will only create two concrete classes of each ingredient type, and we will use buttons and a text view to display the output rather than a recycler view. Simply follow these steps to create our sandwich builder pattern:
Begin with the following interface:
public interface Ingredient { public String description(); public int kcal(); }
Create these two abstract implementations of
Ingredient
. They are empty...