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