PHP
  Home arrow PHP arrow Page 2 - The Basics of Using the Factory Patter...
Dev Shed Forums 
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 
Moblin 
JMSL Numerical Library 
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

The Basics of Using the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-06-25

    Table of Contents:
  • The Basics of Using the Factory Pattern in PHP 5
  • Developing some basic factory classes
  • Defining some array processing classes
  • Testing the functionality of the factory pattern

  • 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


    The Basics of Using the Factory Pattern in PHP 5 - Developing some basic factory classes


    (Page 2 of 4 )

    To start demonstrating the neat functionality provided by the factory pattern, I'll define a few basic factory classes whose primary task will consist of returning different types of array objects to client code according to the concrete specifications of a given programming environment.

    As you'll see, these concrete factories will be capable of "factoring" array objects that will work with both numeric and associative indexes. But let's get away from theory for a moment and see how these brand new factory classes look. Their respective signatures are as follows:

    // define abstract 'ArrayFactory' class
    abstract class ArrayFactory{
      
    abstract public function createArrayObj($type);
    }

    // define concrete factory to create numerically-indexed array
    objects
    class NumericArrayFactory extends ArrayFactory{
      
    private $context='numeric';
      
    public function createArrayObj($type){
        
    $arrayObj=NULL;
        
    switch($type){
          
    case "uppercase";
            
    $arrayObj=new UppercasedNumericArrayObj();
             
    break;
          
    case "lowercase";
            
    $arrayObj=new LowercasedNumericArrayObj();
            
    break;
          
    default:
            
    $arrayObj=new LowercasedNumericArrayObj();
             
    break; 
         
    }
         
    return $arrayObj;
       
    }
    }

    // define concrete factory to create associative array objects
    class AssociativeArrayFactory extends ArrayFactory{
      
    private $context='associative';
      
    public function createArrayObj($type){
        
    $arrayObj=NULL;
        
    switch($type){
          
    case "uppercase";
            
    $arrayObj=new UppercasedAssociativeArrayObj();
            
    break;
          
    case "lowercase";
            
    $arrayObj=new LowercasedAssociativeArrayObj();
            
    break;
          
    default:
            
    $arrayObj=new LowercasedAssociativeArrayObj();
            
    break; 
         
    }
        
    return $arrayObj;
       
    }
    }

    As demonstrated above, the previous concrete factory classes (excepting logically the first one, which has been declared abstract) implement the required business logic to create different types of array objects in accordance with the specifications of a given environment.

    In consonance with the previous concept, the first factory class, which is called "NumericArrayFactory," is responsible for spawning three different types of numeric array objects.  All of them are obviously conceived to work in a "numeric" context.

    Now, concerning the definition of the second concrete factory, you can see that this one returns only associative array objects to client code, which logically must conform to the specifications of an "associative" environment.

    Undeniably, after studying the respective signatures of the two previous factory classes, you'll realize that implementing the factory pattern with PHP is actually an easy-to-grasp process that can be performed with minor hassles.

    So far, so good, right? At this stage I demonstrated how to build a couple of concrete factory classes that are tasked with spawning different types of array objects. So what's the next step? Well, as you learned before, these array objects obviously are spawned from a bunch of concrete classes, so in the following section I'm going to show you their respective signatures. You'll see more easily how the different classes used to implement the factory pattern are linked with each other.

    To learn how these array-related classes will be defined, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Indeed, in the terrain of pattern-based programming, certainly the factory pattern...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





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