-
Book Overview & Buying
-
Table Of Contents
Android Design Patterns and Best Practice
By :
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.
One might think that properties such as toasted and open could be included as part of the bread class, but this itself can lead to increasingly unwieldy code. Say that we want bread and filling to inherit from the same class, say ingredient. This would make sense as they...