Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

phpPatterns:
The Decorator Pattern
Jan 29, 2003 @ 13:57:44

The phpPatterns.com website has a new pattern posted this morning - the Decorator Pattern

According to their site: The decorator pattern provides us with a mechanism for adding functionality to objects at runtime, as an alternative mechanism to creating additional child classes.

Basically, this pattern can help you do things to objects that have already been created, rather than scraping the whole thing and getting a new one. The example they give is a pretty clear "file listing" display class, that allows you to sort on name, type and file's size. The Decorator class just extends the FileList class, and overwrites some of the methods in the FileList to create the sort. Of course, as an example near the end of the article, they show you an alternative that doesn't overwrite the parent's methods to get the job done, and, instead, just adds another "layer" between it and the main FileList class.

tagged:

Link:


Trending Topics: