Adding a decorator
As we know, one of the best ways to add further functionality is with decorator patterns. We have already seen how these work, and now we can add one to our simple sandwich builder. Individual decorations are almost identical in structure, differing only in the values they return, so we need to create only one here, by way of example.
Attaching the pattern
Follow these steps to add the option to offer a toasted sandwich:
Open the empty
Bread
class and complete it like so:public abstract class Bread implements Ingredient { String decoration; int decorationKcal; public String getDecoration() { return decoration; } public int getDecorationKcal() { return decorationKcal; } }
Create a
BreadDecorator
class like the one found here:public abstract class BreadDecorator extends Bread { public abstract String getDecoration(); public...