Tuesday, February 19, 2008

Decorating a CD (the Decorator Pattern)

The other day I implemented the Decorator pattern on a baby shower gift.

Sometimes those of us in the software engineering fields end up describing stuff in our own lingo and make it very difficult to translate what it is we're doing to our PMs, direct managers or customers. So when an example comes along in every day life to help explain certain concepts, it's helpful to jump on that to help draw a picture.

So as I said, the other day I implemented the Decorator pattern on a baby shower gift.

First the background. A coworker and friend I have worked with for a good six or seven years recently was blessed with his first child. We'll call him Brad. About a month prior to the date, I discovered the Rockabye Baby collection of rock goes lullabye. When I saw the Metallica album I knew it would be a great fit. After giving it a listen, with a lend of the Columbus Library and determining it was really well done and entertaining, a group of us pitched in and got a last minute pseudo-gag gift.

Now, being computer geeks, we didn't have anything handy to adorn this gift with. We could have handed the gift to him with no wrapping, but that wouldn't feel like a gift. We needed to decorate the gift. Commonly we do this by using special paper for this, but we didn't have that either. So we used a hunter green filing folder cut up and taped lovingly around the CD.

This is the first application of the Decorator pattern. Decorators by their very nature wrap an existing object and provide additional functionality. In this case our object was the CD. Our Decorator was a file folder. The additional functionality was gift wrapping. Obviously it's a bit easier to decorate any physical object, while in software you have to write a bit of code to engulf the object you wish to decorate.

What is handy is, when you decorate an object in something like Java, is that you can decorate it again. This was applicable to our gift as well. When digging through some documents, one of my co workers, we'll call her Lori, found an old specification for a project Brad had put a lot of work into for over a year. She took the cover off of the spec, and we used that paper to wrap the CD again. It was funnier than the file folder. So we decorated the CD again.

When decorators are created, they use the object they are decorating as the argument for creation. In order to decorate something, you need to have that object. So we pass the decorated CD, which is still by it's very nature a CD into a new decorator. We now have a doubly decorated CD with even more wrapping features!

We applied a third decorator to add some bows we found in Lori's desk, and a fourth to add a To and From label.

Granted, we could have written one large Decorator to do all four things, but we didn't know we needed to do them at the time. The Decorator pattern provides the flexibility to add features when you need them, rather than needing to know all about it up front.

This example somewhat oversimplifies things here and there. Even so, it's a good way to explain it to someone whose idea of a Decorator are the designers on Trading Spaces.

1 comment:

Sqiar BI said...

Thanks a lot for sharing this with all folks you really recognise what you are talking about! In this complex environment business need to present there company data in meaningful way.So user easily understand it .Sqiar (http://www.sqiar.com/) which is in UK,provide services like Tableau and Data Warehousing etc .In these services sqiar experts convert company data into meaningful way.

ShareThis