Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory class with object initialization - trying to avoid static

I'm trying to design a set of factory classes for our system, where some objects created by the factory also need to be initialized before they can be used properly.

Example:

$foobar = new Foobar();
$foobar->init( $qux, ... );
// $foobar ready for usage

For the same of example, lets say that the $qux object is the only dependency that Foobar needs. What I'd like to get to is:

$foobar = Foo_Factory( 'bar' );

In order to avoid the need to pass along the $qux object across the whole system and pass it to the factory class as another parameter, I'd like to perform initialization of Foobar directly in the factory class:

class Foo_Factory {

    public static function getFoo( $type ) {

        // some processing here
        $foo_name = 'Foo' . $type;
        $foo = new $foo_name();
        $foo->init( $qux );

        return $foo;
    }

}

There are few solutions that come to mind, but none of them is ideal:

  1. Add a static setter method for $qux to the factory class, and let it store a reference to $qux in a private static variable. The system can set $qux at the start, and the factory class can prevent any future changes (for security reasons).
    Although this approach works, using a static parameter for storing reference to $qux is problematic during unit testing (e.g. it happily lives survives between individual tests due to its static state).
  2. Create a new context class using Singleton pattern and let the factory class use it to get a reference to $qux. This might be a bit cleaner way to do this than option #1 (although we move the static problem from the factory class to the context class).
  3. Use dependency injection all the way, i.e. pass $qux to any object which uses the factory class, and let that object pass it along to the factory class as another parameter: Foo_Factory::getFoo($type, $qux);.
  4. Same as above (#3), but instead of passing $qux along the system, pass an instance of the factory class instead (i.e. in this case it would not be static, but instantiable).

What would you recommend please? Any of the four alternatives mentioned above, or is there a better way to do this please?

Note: I don't want to get into a static is evil flamewar here, just trying to come up with the best solution.

like image 775
MicE Avatar asked Mar 30 '11 13:03

MicE


2 Answers

I'd go with Dependency Injection all the way. But, instead of passing $qux along everywhere, just register it in the Dependency Injector Container and let the container sort it out. In Symfony Component speak:

// Create DI container
$container = new sfServiceContainerBuilder();

// Register Qux
$container->setService('qux', $qux);
// Or, to have the DI instanciate it
// $container->register('qux', 'QuxClass');

// Register Foobar
$container->register('foobar', 'Foobar')
          ->addArgument(new sfServiceReference('qux'));

// Alternative method, using the current init($qux) method
// Look! No factory required!
$container->register('altFoobar', 'Foobar')
          ->addMethodCall('init', array(new sfServiceReference('qux')));
like image 77
Sander Marechal Avatar answered Oct 01 '22 19:10

Sander Marechal


I'd just make the Factory methods non static and pass it to every object that need that factory.

To set up the factory you'd need to feed it with your $qux parameter in the constructor.

class Foo_Factory {

    public function __construct($qux) {
        $this->qux = $qux;
    }

    public function getFoo( $type ) {
        // some processing here
        $foo_name = 'Foo' . $type;
        $foo = new $foo_name();
        $foo->init( $this->qux );

        return $foo;
    }
}

With that approach you should get the easy of use in the classes where you need to work with the factory without the "trouble" of passing around a service container or a registry.

For this example I'd use the direct approach of just passing around the objects your really need and not abstract that into Container classes.

The decision whether to use a DIC or a Registry or plain old DI is something that, i think, should be done for your whole project. I strongly prefer a DIC over a Registry but like normal DI even better. For the given context it's hard to argue for or against a certain approach.

To sum my point up: If the static factory is the problem just make it non static.

Hope i understood your post right ;)

like image 28
edorian Avatar answered Oct 01 '22 20:10

edorian