Explain this singleton factory pattern

class ConnectionFactory
{
    private static $factory;
    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory(...);
        return self::$factory;
    }

    private $db;

    public function getConnection() {
        if (!$db)
            $db = new PDO(...);
        return $db;
    }
}

function getSomething()
{
    $conn = ConnectionFactory::getFactory()->getConnection();
    .
    .
    .
}

      

Source

There are a few things I don't get

  • How can I say that "the staic property of the class can be accessed without initializing the class"
  • what does !db

    do
  • How does this happen ConnectionFactory::getFactory()->getConnection();

  • Can anyone explain the method getFactory

+3


source to share


4 answers


  • are you here.
  • ! is not. That means if $ db is false then initialize it. Since its in a static method it will stay initialized and next time there will be d $ db existence from this second time! $ Db == false.
  • as for $ db, it checks if an instance of $ factory exists, if it is not created and will not return one, otherwise it will return an existing one.

4.

public static function getFactory()
{
     if (!self::$factory) // is self::$factory initialised?
            self::$factory = new ConnectionFactory(...); //initialise it
     return self::$factory; //return self::$factory
}

      



Also here the $ factory appears to be a variable that is set somewhere in eles. Presumably it can contain a couple of different class names. Nothing changes the way the function works. Its a singleton pattern

Adding an interesting link about this wikipedia template

+3


source


For 1.yes static property of a class is always available with ClassName :: $ staticvarname

For 2. there is definitely a bug here. it should be if (! $ this-> db) and all the code in getConnection should use $ this-> db instead of $ db.



getFactory () here is an "alias" for the more standard getInstance () Singleton template. It returns a single instance of the class if it exists and creates one instance if it doesn't exist.

The getFactory () -> getConnection () call is simple: getFactory () returns a single instance of the ConnectionFactory class. So now that you have a Singleton instance, you can make any call on it. The getConnection () function returns a "single" PDO instance that is processed by the singleon (stored in the $ db member)

+1


source


  • Right
  • ! is the NOT operator. Basically if (! $ Db) means if $ db is null then execute if block
  • Get factory is a function that returns a (single) instance of ConnectionFactory, this instance has getConnection () function
  • There should only be one instance of the class in the singleton pattern, which is a private static member of the class itself. The getFactory () function is a way to get a reference to this single instance (it initializes the instance first if it is zero).
+1


source


  • Yes, static means it will be the same value for every object that requests it, so no instantiation is required
  • ! means NOT

    . It is most often referred to as! =, Denoting NOT EQUAL

    . In this case, it is checked if the db object will be created, so this means that db NOT NULL

    .

3 and 4. The method you specified tells you to use the class (without creating it) and call getFactory (). Which will create a factor if it is NOT NULL

, otherwise it will return an already created factory. After that, it requests a connection using the instance

factory that was returned. Since there is only one factory, we can assume that after the connection has been created (just like the factory was created), then that connection will be available for all uses of the factory going forward.

I suggest reading the Wikipedia article article on Singleton patterns. Hope this can be helpful. Also , keep in mind that the singleton pattern is considered more of an anti-pattern and should be avoided. This class seems to me like it can be just as easily created as a static class with a static getConnection method. With the code displayed, it seems like creation itself is pointless here. But this is only in this small context.

+1


source







All Articles