The most awesomest design pattern, ever to exist and that ever will exist!

At my current day job, someone came up with this design pattern and has implemented it in almost every class throughout the entire application, there are probably close to 100 implementations of this “pattern”. I had to post it because it’s just so awesome!

Firstly let’s late a look at the traditional factory method, it’s usually a static method that instantiates 1 or more objects and returns you the object.

class MyClass
{

    public static function factory($some, $params)
    {
        $it = create_some_object($some, $params);
        return $it;
    }

}

Simple and effective.

Behold! The tri-class factory method, 3 times more awesome than the normal factory method!

Firstly create your class as normal:

class MyClass
{
    ...code...
}

Then create a factory class, (you’re thinking, “what???”, but don’t think, just do):

class MyClassFactory
{
    public static $factory = null;
    ...static methods...
}

Note, some very important features, you must have the public static $factory variable, and it must be initialised to null, despite this being the default behaviour of PHP. The methods also follow a very important structure too, but we’ll get to this in a moment.

Finally, the 3rd and final class, the “Factory Impl” class:

class MyClassFactoryImpl
{
    ...non-static methods...
}

Then to put everything together we need to assign an instance of “Factory Impl” to the static $factory variable in the Factory class, this should be down outside of all the object structures, for added coolness:

MyClassFactory::$factory = new MyClassFactoryImpl;

Confused? You should be. But this is just the start of the awesomeness.

Next, you need to implement the factory methods, they should be implemented as normal public methods in the “Factory Impl” class, e.g.

public function getById($id)
{
    return new object($id);
}

For each method you create, you also need a static function to call this method in the factory class:

public static function getById($id)
{
    return self::$factory->getById($id)
}

Repeat for all your factory methods, and you’re done!

I would recommend that to make it as helpful as possible for other developers, make sure you put everything in one file and try and call the file something different from any of the class names you’ve used, this will ensure that they will probably have to use a require_once each time they use it as it’ll confuse most autoloaders.

You should end up with something like this:

class MyClass
{

    public function __construct($id = null) {}

    public function someMethod() {}

    public function someOtherMethod() {}

}

class MyClassFactoryImpl
{

    public function getEmpty()
    {
        return new MyClass;
    }

    public function getById($id)
    {
        return new MyClass($id);
    }

}

class MyClassFactory
{

    public static $factory = null;
   
    public static function getEmpty()
    {
        return self::$factory->getEmpty();
    }

    public static function getById($id)
    {
        return self::$factory->getById($id);
    }

}

MyClassFactory::$factory = new MyClassFactoryImpl;

There is no denying the awesomeness of this pattern, 3 times more objects, 3 times more memory, 3 times bigger stack, 10 times more code, god knows how many times more inefficient, but infinitely more fun! 😀

Next week, stay tuned for the “super god” class!

Share

Leave a Reply