Using the decorator pattern you can build functionality by stacking objects, which allows objects to dynamically add behaviors to others.
The next diagram is helpful to understand the Decorator pattern:
This is how aggregation stack looks like. The Object A is a base object because it doesn’t contain another object. It will have its own set of behaviors. Object B aggregates Object A allowing Object B augment the behaviors of Object A. We can continue add objects to stack where Object C aggregates Object B and augment behaviors of Object B. The relation is always 1 to 1 in decorator design pattern in order to build up the stack so that one object is on top of another.
To achieve an overall combination of behavior for the stacked objects, you would call upon the top element which is object C, then C -> B -> A. Object A responds with its behavior, then object B adds its incremental behavior and object C adds its incremented behavior.
Continue reading “Decorator Pattern” →