Bech on Enterprise Java
Thursday, May 12, 2005
  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 !
 
Comments: Post a Comment



<< Home
I hereby promise to blog my thoguhts and views on Enterprise java, design patterns, frameworks and all other things that make life as a software developer interesting.

Name:
Location: Oslo, Oslo, Norway

www.glennbech.com

ARCHIVES
May 2005 / June 2005 / July 2005 / January 2006 / February 2006 / June 2006 / July 2006 / August 2006 / October 2006 / November 2006 / March 2007 /


Powered by Blogger