PHP
  Home arrow PHP arrow Page 4 - 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 - Understanding how the strategy pattern works: creating an example


    (Page 4 of 4 )

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 4 hosted by Hostway
    Stay green...Green IT