PHP
  Home arrow PHP arrow Page 2 - Introducing the Strategy Pattern
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introducing the Strategy Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-02-27

    Table of Contents:
  • Introducing the Strategy Pattern
  • A basic example of the design strategy pattern: building a file data handling class
  • Establishing a formatting strategy: defining a contextual class
  • Understanding how the strategy pattern works: creating an example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing the Strategy Pattern - A basic example of the design strategy pattern: building a file data handling class


    (Page 2 of 4 )

    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


       · Over the course of this first article of the series, you'll learn the key concepts ...
       · This is a good example of the strategy pattern, my only gripe is with your...
       · Hi James,Thanks again for the kind comment on my PHP article. Well, as you know,...
       · I noticed that the implementation of this design pattern is actually... wrong....
       · Thanks for the comments. Well, I disagree when you that my implementation of the...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT