SchweppesAle Posted February 17, 2010 Share Posted February 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted February 17, 2010 Author Share Posted February 17, 2010 bump? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 17, 2010 Share Posted February 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted February 17, 2010 Author Share Posted February 17, 2010 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(). Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted February 17, 2010 Author Share Posted February 17, 2010 wait...lol, sorry didn't read through the whole thing. Yea, I guess that would be an advantage. I'll leave it as is then without the private construct, thanks for the feedback. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 17, 2010 Share Posted February 17, 2010 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? Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted February 17, 2010 Author Share Posted February 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.