As I stated in the section that you just read, below I set up a couple of illustrative examples. These show in a friendly fashion how all the classes that you learned before can be put to work together, in this way demonstrating the functionality provided by the strategy design pattern. That being said, please have a look at the following pair of code listings: (example formatting data file as HTML) try{ (example formatting data file as XML) try{ As you can see, the first example uses the "StrategyHTML" class to format file data as HTML, while the second example returns this data to calling code as XML. For reasons of clarity, I broke the two examples into different pieces of code, but if you don’t need to use the "header()" PHP function, you can merge them into one single code block. Final thoughts In this first part of the series, I introduced the basic concepts concerning the implementation of the strategy pattern in PHP 5. As you saw, this pattern can be used in multiple contexts, and also utilize a wide range of predefined strategies. In the second (and last) installment of the series, I’m going to show you how to use this neat pattern for a more useful purpose: validating user-supplied data. You won’t want to miss it!
blog comments powered by Disqus |