ASPit - Totally ASP
 

Go Back   PHPit Forums > General PHP > Advanced Topics
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-06-2006, 02:02 PM   #1
Craige
Ambitious Member
 
Join Date: Mar 2006
Location: Ont. Canada
Posts: 56
Send a message via AIM to Craige Send a message via MSN to Craige
Default 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
Craige is offline   Reply With Quote
Old 07-06-2006, 02:28 PM   #2
rvdavid
Ambitious Member
 
rvdavid's Avatar
 
Join Date: Jun 2006
Location: Sydney, Australia
Posts: 26
Default

PHP Code:
function getSingleton()
{
    return 
Singleton::getInstance();


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/
rvdavid is offline   Reply With Quote
Old 07-06-2006, 09:11 PM   #3
Craige
Ambitious Member
 
Join Date: Mar 2006
Location: Ont. Canada
Posts: 56
Send a message via AIM to Craige Send a message via MSN to Craige
Default

Quote:
Originally Posted by rvdavid
PHP Code:
function getSingleton()
{
    return 
Singleton::getInstance();


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,

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
Craige is offline   Reply With Quote
Old 07-06-2006, 09:58 PM   #4
ironikart
Zealous Member
 
ironikart's Avatar
 
Join Date: Dec 2005
Location: Canberra, Australia
Posts: 103
Default

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.

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
ironikart is offline   Reply With Quote
Old 07-07-2006, 10:50 AM   #5
rvdavid
Ambitious Member
 
rvdavid's Avatar
 
Join Date: Jun 2006
Location: Sydney, Australia
Posts: 26
Default

Hi,

Quote:
Originally Posted by Craige
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.

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:
class Singleton 
{
    
private static $instance false;

    
private __construct()
    {

    }

    
public static function getInstance()
    {
          if (!
self::$instance) {
             
self::$instance = new Singleton();
          }
          return 
self::$instance;
    }


What is it about this implementation that does not work in your favour?



regards,
__________________
R. Villar David
Web Developer
http://rvdavid.blogspot.com/
rvdavid is offline   Reply With Quote
Old 07-10-2006, 11:07 PM   #6
Craige
Ambitious Member
 
Join Date: Mar 2006
Location: Ont. Canada
Posts: 56
Send a message via AIM to Craige Send a message via MSN to Craige
Default

Quote:
Originally Posted by rvdavid
Hi,

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.


Yeah, I probably phrased it wrong

Quote:
Let's start with the subject matter - What exactly are you trying to achieve?

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:
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?


What is it about this implementation that does not work in your favour?

regards,

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:
public static function loadModules/* List of modules to be loaded */ )  {
    
$argNum func_num_args();
    
$argVal func_get_args();


    for ( 
$i 0$i <= $argNum; ++$i )  {

      
// If the paramater has a s_ starting it, the class is a singleton, and special care is needed.
      
if ( ($tmp strpos($argVal[$i], 's_') ) )  {
        
$argVal[$i] = substr($tmp2);
        
$singleton true;
      }

      
$file $modulePath $argVal[$i] . '.mod.php';

      if ( 
file_exists($file) )  {
        if ( @include_once 
$file )  {

          if ( 
$singleton == true )  {
            
$this->registry[$argVal[$i]] = $argVal[$i]::instalize();
            
$singleton false// reset singleton variable
          
}
          else  {
            
$this->registry[$argVal[$i]] = new $argVal[$i]();
          }

        }
      }
      else
        
throw new exception('Module file \'' $file '\' not found'1.2);


    } 
// End For Loop

    
return true;
  } 
__________________
// MustyWindows - Break Through The Windows!
// AmpFusion <- Coming Soon
// CrystalHosting.net

Last edited by Craige : 07-10-2006 at 11:09 PM.
Craige is offline   Reply With Quote
Old 07-17-2006, 01:33 AM   #7
rvdavid
Ambitious Member
 
rvdavid's Avatar
 
Join Date: Jun 2006
Location: Sydney, Australia
Posts: 26
Default

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/
rvdavid is offline   Reply With Quote
Old 07-17-2006, 03:53 AM   #8
ironikart
Zealous Member
 
ironikart's Avatar
 
Join Date: Dec 2005
Location: Canberra, Australia
Posts: 103
Default

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
ironikart is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 09:43 AM.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2006, Jelsoft Enterprises Ltd.