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
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.

Link to comment
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.

 

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.