Patterns: Decorator. A Real life example
The book Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma et al, classifies the Decorator pattern as a structural pattern. This pattern offers a flexible alternative to subclass. The main difference between subclassing and the Decorator pattern is this: with subclassing, you work with the class, whereas in the Decorator pattern, you modify objects dynamically. When you extend a class, the change you make to the child class will affect all instances of the child class. With the Decorator pattern, however, you apply changes to each individual object you want to change.
Need a very tangible and real example ?
Car suppliers often attach license plates to framings with their brand name printed on it. This framing, with the plate attached, are then again mounted on the car. In This way everyone can see where you bought your brand new car. If you're real cool you replace it with a custom one, like the one's show here from http://www.autolicenseframes.com
The framing is just a few centimeters wider than the license plate. The license plate fits into the framing. The bolt holes in the frame and the license plate are perfectly aligned with the holes in the car. This can be viewed as the interface or contract.
The framing can therefore be said to comply to this
interface and it
wraps the license plate. The framing adds functionality (aestethics) to the license plate without modifying it. No subclassing needed, in other words a real life decorator !