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:

  1. 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; 
  2. Create a BreadDecorator class like the one found here:
    public ...

Get Android Design Patterns and Best Practice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.