07-06-2006, 02:02 PM | #1 |
Ambitious Member
|
Factory Pattern and Singleton
How would you recomend I use factory method to create a Singleton object? With the singleton pattern you have a static instalizer function and a private construct. This does not exactly work in my favor. I am not sure how, or if it is even possible, to go about this. Andybody else know?
__________________
// MustyWindows - Break Through The Windows! // AmpFusion <- Coming Soon // CrystalHosting.net |
07-06-2006, 02:28 PM | #2 |
Ambitious Member
Join Date: Jun 2006
Location: Sydney, Australia
Posts: 26
|
PHP Code:
This is done to avoid peppering your code with static calls. Factory methods keep the assembly abstracted. So it gives you the flexibility to refactor later on, like when you decide an object isn't going to be a singleton anymore Regardds,
__________________
R. Villar David Web Developer http://rvdavid.blogspot.com/ |
07-06-2006, 09:11 PM | #3 | |
Ambitious Member
|
Quote:
Well thank you, but I am not sure that helped me at all. That really did not answer my question about how to instalize a singleton pattern class throgh a foctory method call.
__________________
// MustyWindows - Break Through The Windows! // AmpFusion <- Coming Soon // CrystalHosting.net |
|
07-06-2006, 09:58 PM | #4 | |
Zealous Member
Join Date: Dec 2005
Location: Canberra, Australia
Posts: 103
|
Quote:
rvdavid's example is exactly what you are asking. Singleton::getInstance() returns an instance of the singleton, which is returned via the factory method. If you use: $singleton = $myclass->getSingleton() Then you have effectively called Singleton::getInstance();
__________________
MySource Matrix - Open Source CMS http://matrix.squiz.net |
|
07-07-2006, 10:50 AM | #5 | |
Ambitious Member
Join Date: Jun 2006
Location: Sydney, Australia
Posts: 26
|
Hi,
Quote:
You've probably phrased the question incorrectly somehow. Or we've not understood it correctly. I know what it's like so don't worry about it. Let's start with the subject matter - What exactly are you trying to achieve? Maybe you are looking for other ways to implement the Singleton pattern in PHP? If so, what's wrong with having a static instance variable, private constructor and static instance accessor method?
PHP Code:
What is it about this implementation that does not work in your favour? regards,
__________________
R. Villar David Web Developer http://rvdavid.blogspot.com/ |
|
07-10-2006, 11:07 PM | #6 | |||
Ambitious Member
|
Quote:
Yeah, I probably phrased it wrong
Quote:
Instalize a singleton class, through a factory mathod class. However, the singleton class is mixed among an assortment of other classes aswell in the factory method
Quote:
Nope That isn't it. I ended up working out a rather simple way to get this done. Here is the code (untested), do you see what I was trying to do now?
PHP Code:
__________________
// MustyWindows - Break Through The Windows! // AmpFusion <- Coming Soon // CrystalHosting.net Last edited by Craige : 07-10-2006 at 11:09 PM. |
|||
07-17-2006, 01:33 AM | #7 |
Ambitious Member
Join Date: Jun 2006
Location: Sydney, Australia
Posts: 26
|
ahh a dynamic loader/container huh?
I understand now One thing though, why are you putting a singleton in the registry? couldn't you just do a check if it exists in the registry? If it exists return the instance from the registry if not, it creates an instance assigns it to the registry and returns the instance from the registry. What are your thoughts on this? regards,
__________________
R. Villar David Web Developer http://rvdavid.blogspot.com/ |
07-17-2006, 03:53 AM | #8 |
Zealous Member
Join Date: Dec 2005
Location: Canberra, Australia
Posts: 103
|
Yeah, there's no point putting a singleton in the registry - defeat's the purpose. Your registry should handle loading single instances.
__________________
MySource Matrix - Open Source CMS http://matrix.squiz.net |
Thread Tools | Search this Thread |
Display Modes | |
|
|