Categories
Design Patterns Programming

Design Patterns – The Decorator

Tonight was the second night of my design patterns meeting I hold with some friends from work. Most of us already had the book Head First Design Patterns by O’Reilly Publishing, but never made the time to go through it. Jennifer was the one who came up with the idea to start having a meeting every two weeks and we all agreed that it was a good idea.

We’re currently working on our format, but so far have settled on one person being the ‘teacher’ for the evening. This person is responsible for presenting the highlights of the chapter to everyone even though we have all read it. After they are finished presenting, we then go around the table and share our ideas on how the pattern could be used at work or on personal projects.

Tonight we discussed the Decorator Pattern. Here were my solutions:

Recipe card

In this design, the recipe card is the base object and the decorators are ingredients. As per the design, ingredients would be stacked up to create the ingredient list. Here’s a rough cut at the class diagram.

decorator_class.png

click to view a larger image

Some of the interesting characteristics of recipes is that they often have to be scaled (eg, from 4 to 12 persons) or translated to another type of measurement (eg, metric/english). This functionality can easily be added down at the ingredient level. The different ingredients could also have superclasses based on their type – liquid, solid, etc. This makes it easy to separate scaling and measurement translation as necessary.

Invoice discounts

In this example the base object is an invoice and the decorators would be different types of discounts or rebates. This would allow a sales team to be creative with the types of discounts given to a particular client.

Discount objects could be applied to different products, plus you have them expire at certain times (free for 3 months), also be percentages or flat dollar amounts.

Next time I’m presenting the Command Pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *