Jump to content

Exploring Singleton/Factory pattern


SchweppesAle

Recommended Posts

Hi, I was hoping someone could explain to me the negative consequences of implementing the singleton pattern into a factory class. 

 

Here's the code I just started working on:

 

<?php

class bfactory
{

private static $getGenerator;
private static $getParser;

public static function getGenerator()
{
	require_once('kind8_generator.php');
	if(!isset(self::$getGenerator))
	{
		self::$getGenerator = new generator();
	}

	return self::$getGenerator;
}

public static function getParser()
{
	require_once('parse_url.php');
	if(!isset(self::$getParser))
	{
		self::$getParser = new parseURL();
	}

	return self::$getParser;
}

}

?>

 

From what I've read, the process of creating new instances of a single class is very costly in terms of performance so I figured doing something like this would be a positive. 

 

However, I've also been reading a lot regarding how this can have a negative impact on maintenance, etc. 

 

Was hoping someone could provide a few examples of how this could backfire before I go any further.  Thanks

Link to comment
https://forums.phpfreaks.com/topic/192421-exploring-singletonfactory-pattern/
Share on other sites

Eh.  I guess one downside is that the classes are not true singletons and you leave your application with the ability to create multiple instances of them.  However that can be an advantage as well.  Most of the time database classes are implemented as singletons but I have applications that require multiple database objects to exist, due to dealing with multiple databases.

 

I don't really see any maintenance problems with your approach.  I'd be curious about what you read that hints at maintenance problems with such an approach.

 

In the long run it's almost the same code you'd write if they were true singletons.

Eh.  I guess one downside is that the classes are not true singletons and you leave your application with the ability to create multiple instances of them.  However that can be an advantage as well.  Most of the time database classes are implemented as singletons but I have applications that require multiple database objects to exist, due to dealing with multiple databases.

 

I don't really see any maintenance problems with your approach.  I'd be curious about what you read that hints at maintenance problems with such an approach.

 

In the long run it's almost the same code you'd write if they were true singletons.

 

you're right, i forgot the private __construct().

If ClassA is a singleton with a private __construct(), then you can't create a factory ClassB to instantiate the singleton, unless PHP supports friend classes and I've somehow missed them.

 

I'm still curious about the maintenance drawbacks you alluded to from your research.  Do you have any links?

If ClassA is a singleton with a private __construct(), then you can't create a factory ClassB to instantiate the singleton, unless PHP supports friend classes and I've somehow missed them.

 

I'm still curious about the maintenance drawbacks you alluded to from your research.  Do you have any links?

 

 

http://accu.org/index.php/journals/337

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.