Home arrow PHP arrow Page 2 - Introducing the Strategy Pattern

A basic example of the design strategy pattern: building a file data handling class - 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

A good point to begin demonstrating how the strategy pattern works is with developing an adequate context, where you can easily see the functionality provided by the pattern in question.

Therefore, I'm going to build a file data handling class which will be capable of performing some handy tasks, like reading and writing strings of data to a specified file location.

Later on, Iím going to create a contextual class, which will determine programmatically what type of format should be applied to all the data returned by the aforementioned file data handler. In this case, two output formats will be available: HTML and XML. As usual, this condition can be easily modified to expand the capacity of the contextual class.

And finally, after defining all the classes that I discussed earlier, Iím going to establish a strategy between them, so you can see how the aptly-named strategy pattern works. Pretty simple, isnít it?

Now that I have explained how Iím going to apply the strategy pattern here, pay attention to the signature of the following class. As I said before, it is tasked with performing some basic file handling operations. Its structure is as follows:

// define 'FileReader' class
class FileDataHandler{
   private $dataFile;
   private $data;
   public function __construct($data='default data',
$dataFile='default_data_file.txt'){
     if(!$data||!is_string($data)){
       throw new Exception('Invalid string of data!');
     }
     if(!file_exists($dataFile)){
       throw new Exception('Invalid data file!');
     }
     $this->data=$data;
     $this->dataFile=$dataFile;                     
   }
   // write data to file
   public function writeData(){
     if(!$fp=fopen($this->dataFile,'w')){
       throw new Exception('Error opening data file!');
     }
     if(!fwrite($fp,$this->data)){
       throw new Exception('Error writing data to file!');
     }
     fclose($fp);
   }
   // read data from file
   public function readData(){
     if(!$contents=file_get_contents($this->dataFile)){
       throw new Exception('Error reading data from file!');
     }
     return $contents;
   }
}

As you can see, the signature of the above file data handling class is indeed simple to grasp. As I expressed before, this class exposes some basic methods, which come in handy for reading data from a specified file, as well as writing new contents to it.

Obviously, these operations are carried out by the "readData()" and "writeData()" methods respectively, so until now, the prior "FileDataHandler" class shouldnít present any problems to you.

Also, a possible use for the previous class can be seen by the example below:

try{
   // example using the 'FileDataHandler' class
   $fdHandler=new FileDataHandler();
   $fdHandler->writeData();
   echo $fdHandler->readData();
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Not surprisingly, the output generated by the above example is the following:

default data

Pretty basic, isnít it? Nonetheless, this is where things are getting really exciting. In the section that youíre about to read, Iím going to define a contextual class. It will be responsible for determining what format should be applied to the data read by the previous file data handing class (HTML or XML), in this way implementing a formatting strategy, or whatís commonly called "the strategy pattern."

To see how this contextual class will be created, keep reading.



 
 
>>> 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: