SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Factory Class

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    438
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Factory Class

    I think I understand what a factory class does, I'm just unsure when I'd use it. For example, I have this is my bootstrap:

    PHP Code:
    /**
     * Generic objects
     */                                   
    $clean                 = new Helpers\Clean();
    $page                 = new Helpers\Page();
    $email                 = new Helpers\Email();
    $date                 = new Helpers\Date();
    $seconds             = new Helpers\Seconds();
    $image                 = new Files\Image();


    /**
     * Database connection
     */
    if($settings['DEV']) {

        
    $db = new Database\Connection($settings['DEV_DATABASE_HOST'],
                                      
    $settings['DEV_DATABASE'],
                                      
    $settings['DEV_DATABASE_USER'],
                                      
    $settings['DEV_DATABASE_PASSWORD'],
                                      
    $settings['DEV_DATABASE_CHARSET'],
                                      
    $settings['DEV_DATABASE_LOG']);
                                               
    }
    else {

        
    $db = new Database\Connection($settings['LIVE_DATABASE_HOST'],
                                      
    $settings['LIVE_DATABASE'],
                                      
    $settings['LIVE_DATABASE_USER'],
                                      
    $settings['LIVE_DATABASE_PASSWORD'],
                                      
    $settings['LIVE_DATABASE_CHARSET'],
                                      
    $settings['LIVE_DATABASE_LOG']);

    }


    /**
     * Objects that require dependencies
     */
    $generate             = new Helpers\Generate($clean);
    $format             = new Helpers\Format($clean);
    $validateServer        = new Helpers\ValidateServer($clean$date);
    $validateClient        = new Helpers\ValidateClient($clean);
    $generate            = new Helpers\Generate($clean);
    $query                 = new Database\Query($db$clean);
    $file                 = new Files\File($clean);
    $upload                 = new Files\Upload($file$generate);
    $search                = new Content\Search($db$clean); 
    If I understand factory classes they create a bunch of objects. But why not just have code like the above?

    Finally, I have just found out about ReflectionClass. Does this have any relevance to factory classes or is it just for debugging?

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    There could be a few reasons for using factories. One is when the exact class to instantiate can vary depending on runtime information. For example, let's pretend you have a variety of "Clean" classes, such as HtmlClean, JavaScriptClean, and SqlClean (though, I would name these as a noun "Cleaner" rather than as a verb).

    PHP Code:
    class HtmlCleaner
    {
        public function 
    clean($something)
        {}
    }

    class 
    JavaScriptCleaner
    {
        public function 
    clean($something)
        {}
    }

    class 
    SqlCleaner
    {
        public function 
    clean($something)
        {}
    }

    class 
    CleanerFactory
    {
        public function 
    getCleaner($content)
        {
            if (
    /* some test */) {
                return new 
    HtmlCleaner();
            } elseif (
    /* some test */) {
                return new 
    JavaScriptCleaner();
            } elseif (
    /* some test */) {
                return new 
    SqlCleaner();
            }
        }
    }

    $cleanerFactory = new CleanerFactory();

    // the factory decides which class to instantiate
    $cleaner $cleanerFactory->getCleaner('<html>'); 
    Another example from the wiki page... Pretend you have classes GifReader and JpegReader. Which should be instantiated depends on runtime information, on the kind of image you're trying to read.

    PHP Code:
    class ImageReaderFactory
    {
        public function 
    getImageReader($image)
        {
            if (
    /* some test */) {
                return new 
    GifReader();
            } elseif (
    /* some test */) {
                return new 
    JpegReader();
            }
        }

    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •