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

The decorator design pattern

Regardless of object creation expense, there are still times when the nature of our model will necessitate an unreasonable number of sub-classes, and this is where the decorator comes in extremely handy.

Take the bread in our sandwich app, for example. We would like to offer several types of bread, but in addition, we want to offer the choice of having the bread toasted, the sandwich open, and a selection of spreads. By creating toasted and open versions for each bread type, the project would very soon become unmanageable. The decorator allows us to add functionality and properties to an object during runtime without having to make any changes to the original class structure.

Setting up a decorator

One might think that ...

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