Recently, I have become interested obsessed with design patterns. What caused the spark of excitement was that I was working on an algorithm similar to one of the patterns (I can't remember which) when I read Head First Design Patterns and realized it had already been created for me! By the way, I'd recommend Head First, it's a great learning series.
Also, I use the term "class" for "component" and "method" for "function". So without further ado, my first article on implementing design patterns in Coldfusion.
A decorator more or less wraps a base class and well...decorates it. This can be useful when you have a scenario that would need a lot of classes to cover all of the possible combinations of objects.
I followed the example by Hal Helms as far as the class organization is concerned. Some of the decorators are me being funny (piglatin and mirror), but I can see some value in the TableDecorator, JsonDecorator and XmlDecorator.
Let me explain each file a bit more.
- Instantiates the components and outputs results
- Base class (whose methods will be overridden by the decorators)
- Base decorator class (all decorators for Person will inherit from this class)
- Creates a JSON representation of a Person.
- Creates a XML representation of a Person by wrapping the elements with nodes that represent each element
- Creates a table representation of a Person by wrapping elements with <td>'s and the entire Person with <tr>
Wikipedia's Page on Decorators
Hal Helm's Decorator Article