PHP
  Home arrow PHP arrow Page 2 - Factoring Content Boxes with the Factory Pattern 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

Factoring Content Boxes with the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-07-09


    Table of Contents:
  • Factoring Content Boxes with the Factory Pattern in PHP 5
  • Factoring web page content boxes
  • Building different types of content boxes
  • Displaying web page content boxes via the factory pattern

  • 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


    Factoring Content Boxes with the Factory Pattern in PHP 5 - Factoring web page content boxes
    ( Page 2 of 4 )

    Although I must admit that the factory pattern can be used with a great variety of real-world applications, such as form and web page generators, database abstraction layers, and so forth, in this case I'm going to demonstrate how to utilize its neat functionality to build different types of content boxes. These boxes can be quickly included into any web document by simply invoking the appropriate factory class.

    Basically, the visual appearance that I plan to achieve with these content boxes is illustrated by the picture below:

    As you can see, the above image shows pretty clearly how distinct kinds of web page content boxes can be easily created by using the factory pattern, in this way demonstrating yet another useful implementation for this popular pattern.

    All right, now that you have seen how the pertinent content boxes are going to look when included into a specific web page, let me show you the respective definition of the factory class that builds them. Here is the signature for the class in question; examine its source code, please:

    class ContentBoxFactory{
       public function createContentBox($type,$title,$contents){
         $contentBox=NULL;
         switch($type){
           case "grey";
             $contentBox=new GreyContentBox($title,$contents);
             break;
           case "blue";
             $contentBox=new BlueContentBox($title,$contents);
             break;
           case "yellow";
             $contentBox=new YellowContentBox($title,$contents);
             break;
           default:
             $contentBox=new GreyContentBox($title,$contents);
             break;               
         }
         return $contentBox;
       }
    }

    See how easy it is to define a basic factory class for building three different types of content boxes? I bet you do! The "ContentBoxFactory" class has been provided with the capacity for returning to client code, a decent variety of content box objects, identified as "GreyContentBox," "BlueContentBox" and "YellowContentBox" respectively.

    Obviously, the main difference between the aforementioned content box objects rests upon their background and title colors, something that can be perfectly seen in the image shown a few lines above.

    Okay, at this point you hopefully grasped the logic that drives the previous factory class, which indeed presents a simple signature. However, since the prior content boxes need to be included into a sample web document to appreciate their real usefulness, I'm going to build a web page generator class. Its primary function will be creating the CSS styles and web page sections required for using respective content boxes.

    Having said that, here is the corresponding definition for this web page generator:

    // define 'WebPage' class
    class WebPage{
       private $title='Testing the factory pattern';
       public function __construct(){
         $this->style=<<<EOD

    <style type="text/css">
    body{
       padding: 0;
       margin: 0;
       background: #fff;
    }

    .greybox{
       float: left;
       width: 200px;
       margin: 10px 0 10px 10px;
       background: #eee;
       border: 1px solid #999;
    }

    .greybox h2{
       padding: 7px 0 9px 0;
       margin: 0;
       background: #ccc;
       font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
       color: #000;
       text-align: center;
       border-bottom: 1px solid #999;
    }

    .bluebox{
       float: left;
       width: 200px;
       margin: 10px 0 10px 10px;
       background: #9cf;
       border: 1px solid #036;
    }

    .bluebox h2{
       padding: 7px 0 9px 0;
       margin: 0;
       background: #09c;
       font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
       color: #000;
       text-align: center;
       border-bottom: 1px solid #036;
    }

    .yellowbox{
       float: left;
       width: 200px;
       margin: 10px 0 10px 10px;
       background: #ffc;
       border: 1px solid #c63;
    }

    .yellowbox h2{
       padding: 7px 0 9px 0;
       margin: 0;
       background: #fc0;
       font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
       color: #000;
       text-align: center;
       border-bottom: 1px solid #c63;
    }

    p{
       padding: 10px;
       font: normal 11px Tahoma, Arial, Helvetica, sans-serif;
       color: #000;
    }
    </style>
    EOD;
       }
       public function createHeader(){
         return '<html><head><title>'.$this->title.'</title>'.$this-
    >style.'</head><body>';
       }
       public function createFooter(){
         return '</body></html>'; 
       }
    }  

    As demonstrated previously, the above "WebPage" generator class presents a few basic methods for creating the different sections of a basic web document. It also generates, via the corresponding constructor, the set of CSS styles required to build all of the content boxes that you saw before.

    So far, so good right? You have learned how to build two complementary classes, that is the factory and the web page generator respectively, aimed at displaying these simple content boxes on the browser. Nonetheless, there are a few missing pieces in this schema, since the classes that render these boxes remain undefined as of yet.

    Now, bearing in mind this issue, in the following section I'm going to show you the complete signatures for these brand new classes, so you can understand more easily how they can be linked with each other.

    To learn how these brand new classes will be built, please click on the link that appears below and keep reading.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek