Benefits of factory pattern?

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Is there any benefits of using factory? Is it considered as bad practice?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Like most things, factories have their benefits and drawbacks. Have you read up on them? Are there any particular problems you're facing that you think would benefit from implementing one?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

One thing that made me consider doing a factory (and why I chose to use it) is using a class with drivers (so to speak).

For instance, I have a database abstract class with drivers... class that extends it... mysqli (with possible add-on drivers like postgreSQL, SQLlite, and so on)... 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... 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.
PHP Code: [ Select ]
$ret = simplexml_load_file(CONFIG_FILE);
$dbase = (string) $ret->dbase;
$db = factory::load($dbase, 'db');
  1. $ret = simplexml_load_file(CONFIG_FILE);
  2. $dbase = (string) $ret->dbase;
  3. $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:
PHP Code: [ Select ]
$captcha_loc = factory::locate($captcha[$type], 'captcha');


And I also have an autoload function set-up in my factory singleton...
PHP Code: [ Select ]
/**
 * 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!');
    }
}
  1. /**
  2.  * public static function register()
  3.  *
  4.  *      Registers the autoload function
  5. **/
  6.  
  7. public static function register()
  8. {
  9.     spl_autoload_register(array(new self, 'autoload'));
  10. }
  11.  
  12. /**
  13.  * public static function autoload( string $class )
  14.  *      @string $class - The name of the class (and the file [mostly]) loaded
  15.  *
  16.  *      The autoload function
  17. **/
  18.  
  19. public static function autoload($class)
  20. {
  21.     // Checking if we are doing it for twig or not
  22.     if(substr($class, 0, 5) == 'Twig_')
  23.     {
  24.         // Setting the class to self
  25.         self::$class = $class;
  26.        
  27.         // Setting the main
  28.         self::$_main = './includes/';
  29.        
  30.         // Retrieving the directory structure in which the file is located in
  31.         // (The classes are written with the directory structure (from the ./includes
  32.         // folder) is in the class name. For instance a class named, Twig_Extension_Core
  33.         // is located in ./includes/Twig/Extension/Core.php).
  34.         $path = explode('_', $class);
  35.        
  36.         // The path to the file in which the class is located
  37.         self::$_file = self::$_main . implode('/', $path) . '.php';
  38.        
  39.         // Checking if the file exists
  40.         if(file_exists(self::$_file))
  41.         {
  42.             // If the file exists, we are returning the filepath
  43.             return include_once(self::$_file);
  44.         }
  45.        
  46.         // The file failed to be included... dying
  47.         die(self::$_file . ' failed to be included!');
  48.     }
  49.     else
  50.     {
  51.         // Setting the class to self
  52.         self::$class = $class;
  53.        
  54.         // Setting the main
  55.         self::$_main = './includes/';
  56.        
  57.         // Our file path
  58.         self::$_file = self::$_main . self::$class . '.php';
  59.        
  60.         // Checking if the file exists
  61.         if(file_exists(self::$_file))
  62.         {
  63.             // If the file exists, we are returning the filepath
  64.             return include_once(self::$_file);
  65.         }
  66.        
  67.         // The file failed to be included... returning false
  68.         die(self::$_file . ' failed to be included!');
  69.     }
  70. }

Not sure if I explained my reasoning well in using a factory, but I think it's working fine.
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Your database idea has already been addressed by PDO. ;)

For your mailer idea, you may also want to look into the Strategy Pattern. With that, generally speaking (since design patterns are purposely half-baked) you'd have one mailer object that would act as a wrapper API to various implementations that can be selected at runtime (and perhaps chosen/generated via a factory).
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

The thing with PDO is, I'm using MySQLi. I'm guessing it's a bit faster then PDO.

Never heard of strategy pattern, though I have thought about doing it that way, but for some reason I wanted to use an abstract class instead, rather then a wrapper (kind of) class that chooses the type of mailer/db to use (as per config file/setting etc).
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Any speed benefits of MySQLi over PDO are likely negligible. And you get the benefit of not having to reinvent the wheel, instead having battle-tested code at your disposal.

Keep in mind, you can use a factory to determine the strategy your strategy object uses.

You might not even need the wrapper if all you want is a consistent API. You can use either an interface or, like you want, abstract class for that.

Just throwing out ideas/things for you to consider. :) There's definitely more than one way to attack the problem, and none are necessarily the 'right' way.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

You're right... none are necessarily the 'right' way, but I'm hoping to find the best way.

One thing about being self-taught is that you miss a lot of stuff when starting to learn programming so when you get to developing programs, even though you know the general idea of how to accomplish something, you don't know the different ways, pros/cons of them, and so on and so forth.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: demonmaestro and 175 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.