PHP
  Home arrow PHP arrow Page 2 - The Basics of Abstract Factory Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

The Basics of Abstract Factory Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-01-24


    Table of Contents:
  • The Basics of Abstract Factory Classes in PHP 5
  • Introducing the abstract factory pattern: defining an abstract web page element factory
  • Creating small and large DIV objects
  • Seeing the abstract factory pattern in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 Abstract Factory Classes in PHP 5 - Introducing the abstract factory pattern: defining an abstract web page element factory
    ( Page 2 of 4 )

    In consonance with the concepts that you just read in the introduction, first I'm going to define an abstract factory class aimed at creating generic web page elements. As you'll see shortly, this class will define what web page objects will be created by two concrete factories, in this way implementing the schema required by the factory pattern.

    Having explained the purpose of defining this abstract factory class  for creating web page objects, here is its signature:

    // define abstract 'WebPageElementFactory' class
    abstract class AbstractWebPageElementFactory{
       abstract public function createWhiteDivElement();
       abstract public function createBlackDivElement();
    }

    As you can see, the abstract factory class defines clearly what type of objects must be created by two additional concrete factories. In this case, these two non-abstract classes will be tasked with spawning two specific kind of objects: white and black DIV elements respectively.

    However, the signature for the above abstract class does not say much about how to implement the abstract factory pattern. Therefore. let me go one step further and derive two subclasses from the abstract one. these will offer a concrete implementation for creating different types of DIV elements.

    Having said that, the respective definitions for these two new subclasses are as follows:

    // define concrete 'SmallDivElementFactory' class
    class SmallDivElementFactory extends AbstractWebPageElementFactory{
       private $content='small';
       public function createWhiteDivElement(){
        return new SmallWhiteDivElement;
       }
       public function createBlackDivElement(){
         return new SmallBlackDivElement;
       }
    }
    // define concrete 'LargeDivElementFactory' class
    class LargeDivElementFactory extends AbstractWebPageElementFactory{
       private $context='large';
       public function createWhiteDivElement(){
         return new LargeWhiteDivElement;
       }
       public function createBlackDivElement(){
         return new LargeBlackDivElement;
       }
    }

    You'll definitely agree with me that things are getting really interesting! Please, notice how the two previous concrete factory classes first define the respective contexts where they're going to work, that is "small" and "large," and then return to client code the correct DIV objects.

    In the first case, the factory class insures that either a black or white DIV object will be always created in the "small" context, while in the second case, objects will be spawned in the "large" environment. Now, are you starting to grasp the logic behind the abstract factory pattern? I hope you are!

    Okay, at this point I have shown you how the abstract factory looks, as well as how the respective concrete factories were defined. Therefore I think it's time to move forward and proceed to create the group of classes that are tasked with generating black and white DIV objects according to the respective contexts.

    Do you want to see how these factory classes will be defined? All right, jump into the following section and keep reading. I'll be there, waiting for you.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek