Home arrow PHP arrow Page 4 - Introducing the Strategy Pattern

Understanding how the strategy pattern works: creating an example - PHP

The strategy design pattern can be very useful in the context of form validation. This article, the first of two parts, will introduce you to the strategy pattern and give you some idea of how you can use it in your own PHP applications.

TABLE OF CONTENTS:
  1. Introducing the Strategy Pattern
  2. A basic example of the design strategy pattern: building a file data handling class
  3. Establishing a formatting strategy: defining a contextual class
  4. Understanding how the strategy pattern works: creating an example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
February 27, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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{
   // instantiate 'FileDataHandler' class
   $fdHandler=new FileDataHandler('This string will be formatted
depending on the context!');
   // write data to file
   $fdHandler->writeData();
   // instantiate 'StrategyHTML' class
   $strategyHTML=new StrategySelector('html');
   // display file contents formatted as HTML
   echo $strategyHTML->displayFileContents($fdHandler);
   // instantiate 'StrategyXML' class
   $strategyXML=new StrategySelector('xml');
   // display file contents formatted as XML
   header('Content-type: text/xml; charset=iso-8859-1');
   echo $strategyXML->displayFileContents($fdHandler);
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

(example formatting data file as XML)

try{
   // instantiate 'FileDataHandler' class
   $fdHandler=new FileDataHandler('This string will be formatted
depending on the context!');
   // write data to file
   $fdHandler->writeData();
   // instantiate 'StrategyXML' class
   $strategyXML=new StrategySelector('xml');
   // display file contents formatted as XML
   header('Content-type: text/xml; charset=iso-8859-1');
   echo $strategyXML->displayFileContents($fdHandler);
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: