Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-settings.php on line 512 Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-settings.php on line 527 Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-settings.php on line 534 Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-settings.php on line 570 Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-includes/cache.php on line 103 Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-includes/query.php on line 61 Deprecated: Assigning the return value of new by reference is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-includes/theme.php on line 1109 Datensarg » Blog Archive » Parameterized Factory Desing Pattern

Parameterized Factory Desing Pattern

Deprecated: Function split() is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-content/plugins/google-analytics-for-wordpress/gapp/googleanalytics.php on line 391 Deprecated: Function split() is deprecated in /var/www/l3s6524/html/datensarg/wordpress/wp-content/plugins/google-analytics-for-wordpress/gapp/googleanalytics.php on line 391

The parameterized factory design pattern is an extension to the factory design pattern as you might have guessed, but here we can make the factory create different objects depending on the parameter passed to the create-method. Imagine your application is supposed to connect to a MySQL-DB or to a PostgreSQL-DB. Instead of creating different objects in your code you might just want to pass “mysql” or “pgsql” to the create-method and you get the class needed for the specific database.

//------------------------------------------------
// Parameterized Factory
//------------------------------------------------
// Definition
class DbMySQL
{
	public $content= "I am MySQL!\n";
}

class DbPgSQL
{
	public $content= "I am PostgreSQL!\n";
}

class DbFactory
{
	public function Create($dbtype)
	{
		switch($dbtype)
		{
			case "mysql":
				return new DbMySQL();
				break;
			case "pgsql":
				return new DbPgSQL();
				break;
		}
	}
}

// Usage
$myFirstObj= MyClassFactory::Create("mysql");
$mySecondObj= MyClassFactory::Create("pgsql");
echo $myFirstObj->content; // "I am MySQL!"
echo $mySecondObj->content; // "I am PostgreSQL!"

Leave a Reply