PHP
  Home arrow PHP arrow Page 4 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class
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 Singleton and Factory Patterns in PHP: a rendering-capable factory class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2005-11-16


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: a rendering-capable factory class
  • The Factory pattern in a real application: a quick look at the “formElementFactory” class
  • A smarter factory: applying Polymorphism within the “formElementFactory” class
  • Polishing the Page

  • 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 Singleton and Factory Patterns in PHP: a rendering-capable factory class - Polishing the Page
    ( Page 4 of 4 )

    Indeed, I can go one step further by polishing the form’s visual presentation, and define a sample class “Page”, which builds the basic structure of a web page. Doing so, we’re able to combine the functionality of this class along with “formElementFactory”, for a quick and dirty implementation. So, the definition for the simple “Page” class is the following:

    class Page {
        private $output;
        public function __construct(){
            $this->output='<html><head><title>Default
    Page</title></head><body><h1>Default Page</h1></body></html>';
        }
        public function makeHeader($header){
            $this->output=$header;
        }
        public function makeBody($body){
            $this->output.=$body;
        }
        public function makeFooter($footer){
            $this->output.=$footer;
        }
        public function getPage(){
          return $this->output;
        }
    }

    In simple terms, the above class presents the classic methods for building the header, body and footer sections of a web page, while its constructor sets up a default sample page. Definitely, I won’t stop explaining its logic. Instead, I’ll show an example that uses this class to generate a login form. Here is the sample code:

    // instantiate a new Page object
    $page=new Page();

    // build header page section
    $page->makeHeader('<html><head><title>Object-Based web
    form</title></head><body>');

    // add form tag and build a table header
    $body='<form action='.$_SERVER['PHP_SELF'].'
    method="post"><table><tr><td>Object-Based web form</td></tr>';

    // build a text input box
    $body.='<tr><td>User ID '.formElementFactory::createElement
    ('textinput',array('name'=>'userid','maxlength'=>'20')).'</td></tr>';

    // build a password field
    $body.='<tr><td>Password '.formElementFactory::createElement
    ('passwordinput',array('name'=>'passwrd','maxlength'=>'20')).
    '</td></tr>';

    // build a submit button
    $body.='<tr><td>'.formElementFactory::createElement
    ('submitbutton',array
    ('name'=>'send','value'=>'Log in')).'</td></tr>';
    $body.='</table></form>';

    // add the code to the body section
    $page->makeBody($body);

    // build the footer page section
    $page->makeFooter('</body></html>');

    // display the page
    echo $page->getPage();

    The above code uses the “Page” class to generate the structure of a web page, by displaying the header, body, and footer parts. Notice the rough usage of the “formElementFactory” class to obtain the code for the required form elements, by decoupling object instantiation from the general form rendering process.

    The only point worth mentioning on the above example is the extensive use of the static “createElement()” method, which is naturally invoked without the need to explicitly instantiate an object from the form element factory class. As you remember, this class has been declared as abstract, therefore any attempt to instantiate an object from it will result in the PHP interpreter triggering a fatal error.

    Despite the fact that I’ve hidden behind the structure of the factory class all of the required processing for displaying form elements, I’m still far away from having an efficient rendering mechanism. Notice that the rough use of the “Page” class is merely an example, not suitable for use in a fully functional application.

    However, the factory class works quite well, by implementing the Factory pattern, for solving annoying tasks involved in displaying programmatically web form elements. Now, I’ve exposed a common problem that comes up when building real-world applications that can be addressed in an effective way, in this case by applying a design pattern.

    Wrapping up

    That’s all about it for now. In this third part of the series, I’ve explained how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects to boost the functionality of the factory class, by simplifying the process for rendering form elements.

    Now, let’s pay attention to the next part of the series, where I’ll explain the correct application of the Singleton pattern, in order to work with only a single instance of a form factory class. As you’ll see in short, making a class a Singleton is quite easy to do, allowing you to write robust PHP4 programs, by reducing the hassles related to manipulating multiple object instances.



     
     
    >>> 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