PHP
  Home arrow PHP arrow Page 4 - Introducing Builder Objects 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? 
PHP

Introducing Builder Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-10-04


    Table of Contents:
  • Introducing Builder Objects in PHP 5
  • Building basic XML documents: definition of the target object
  • Of builders and XML pages: the programmatic creation process
  • Taking control of the process: a director class
  • Putting all the classes to work together

  • 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


    Introducing Builder Objects in PHP 5 - Taking control of the process: a director class
    ( Page 4 of 5 )

    Defining a director class is a procedure closely similar to the one shown when creating the respective builder, therefore I think you should grasp the concept very quickly. As I did before, I’ll begin by defining an abstract class, which will set up the generic structure for all the eventual director objects.

    Having said that, here is the signature of the brand new “AbstractXMLDirector” class. Please take a look at it:

    // define abstract 'AbstractXMLDirector' class
    
    abstract class AbstractXMLDirector{
    
    	abstract function __construct(AbstractXMLBuilder $xmlBuilder);
    
    	abstract function buildXMLPage();
    
    	abstract function getXMLPage();
    
    }
    

    As you can see, the above abstract class simply establishes the skeleton of all the objects that will be created from it. Notice the declaration of two primary methods aside from the respective constructor, called “buildXMLPage()” and “getXMLPage()” respectively.

    In short, these will be responsible for instructing the previous builder about the way that all the XML pages should be created. Now, do you see why this class has been called the "director?" Certainly, it’s a faithful representation with PHP code of your own boss.

    Now, leaving the jokes apart, please take a look at the following class’ source code, which shows a concrete implementation for the prior “AbstractXMLDirector” class:

    // define concrete 'XMLDirector' class
    
    class XMLDirector extends AbstractXMLDirector{
    
    	// accept 'XMLBuilder'object
    
    	public function __construct(AbstractXMLBuilder $xmlBuilder){
         
    		$this->xmlBuilder=$xmlBuilder;
    
    	}
    
    	// build XML page
    
    	public function buildXMLPage(){
          
    		$this->xmlBuilder->addXMLNode('This is node 1');
          
    		$this->xmlBuilder->addXMLNode('This is node 2');
          
    		$this->xmlBuilder->addXMLNode('This is node 3');
    
    		$this->xmlBuilder->addXMLNode('This is node 4');
       
    		$this->xmlBuilder->addXMLNode('This is node 5');
          
    		$this->xmlBuilder->addXMLNode('This is node 6');
    
    	}
    
    	// return XML page to calling code
    
    	public function getXMLPage(){
    
    		header('Content-Type:text/xml');
    
    		return $this->xmlBuilder->getXMLPage();
    
    	}
    
    }
    

    As shown above, now the brand new “XMLDirector” class exposes the corresponding concrete methods to instruct the builder in question on how all the XML pages must be created. In addition, it should be noted how the builder object itself is fed straight into the constructor, which comes in handy for using all its methods.

    Al right, after understanding how the previous director class works, I guess that it’s also easy seeing how the builder pattern can be used for building target objects. In this case, the appropriate combination of the director and builder objects respectively results in the adequate creation of XML pages.

    However, before you finish reading this tutorial, I’d like to show you how all the classes that I defined previously can be used together to generate programmatically some XML pages. Therefore, in the next few lines, you’ll see the builder pattern in action. Thus I suggest you to jump straight into the following section. I’ll be there, waiting for you.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT