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