BO
466 10
Asked
Updated
Viewed
6.1k times

Are there any benefits of using the factory design pattern? Is it considered a bad practice?

add a comment
0

1 Answer

  • Votes
  • Oldest
  • Latest
BO
466 10
Answered
Updated

For me the benefit of using the factory design pattern in my situation is that it allows me to to use a class with drivers (so to speak).

For instance, I have a database abstract class with drivers, a class that extends it, MySQLi (with possible add-on drivers like postgreSQL, SQLlite, and so on), and these drivers are in a different folder.

Or for mail, you could mail via SMTP or regular mail, and the way you would set it up would be different. You have an abstract class in main includes directory and the drivers in the mail/ folder.

The directory tree would look something like:

main/includes/database.php
main/includes/mail.php
main/includes/db/mysqli.php
main/includes/db/postgresql.php
main/includes/db/sqllite.php
main/includes/mail/smtp.php
main/includes/mail/mail.php
main/index.php

Then I use factory to set what I need.

$ret = simplexml_load_file(CONFIG_FILE);
$dbase = (string) $ret->dbase;
$db = factory::load($dbase, 'db');

That would load MySQLi.php in the db folder.

I also created a function in factory class/singleton where I only retrieve the path of the file:

$captcha_loc = factory::locate($captcha[$type], 'captcha');

And I also have an autoload function set-up in my factory singleton...

/**
 * public static function register()
 * 
 *      Registers the autoload function
**/

public static function register()
{
    spl_autoload_register(array(new self, 'autoload'));
}

/**
 * public static function autoload( string $class )
 *      @string $class - The name of the class (and the file [mostly]) loaded
 * 
 *      The autoload function
**/

public static function autoload($class)
{
    // Checking if we are doing it for twig or not
    if(substr($class, 0, 5) == 'Twig_')
    {
        // Setting the class to self
        self::$class = $class;

        // Setting the main
        self::$_main = './includes/';

        // Retrieving the directory structure in which the file is located in
        // (The classes are written with the directory structure (from the ./includes
        // folder) is in the class name. For instance a class named, Twig_Extension_Core
        // is located in ./includes/Twig/Extension/Core.php).
        $path = explode('_', $class);

        // The path to the file in which the class is located
        self::$_file = self::$_main . implode('/', $path) . '.php';

        // Checking if the file exists
        if(file_exists(self::$_file))
        {
            // If the file exists, we are returning the filepath
            return include_once(self::$_file);
        }

        // The file failed to be included... dying
        die(self::$_file . ' failed to be included!');
    }
    else
    {
        // Setting the class to self
        self::$class = $class;

        // Setting the main
        self::$_main = './includes/';

        // Our file path
        self::$_file = self::$_main . self::$class . '.php';

        // Checking if the file exists
        if(file_exists(self::$_file))
        {
            // If the file exists, we are returning the filepath
            return include_once(self::$_file);
        }

        // The file failed to be included... returning false
        die(self::$_file . ' failed to be included!');
    }
}

Not sure if I explained my reasoning well in using a factory, but I think it's working fine.

add a comment
0