O'Reilly logo

Android Design Patterns and Best Practice by Kyle Mew

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required