Question

As I use OO design patterns I want to make sure I am really getting it. I understand about the importance of dependency injection and also about service container/factory objects. I love the idea of a factory method that can inject dependencies into itself when loaded via a static method, returning a copy of itself that is complete. I like how clean this is in the code that consumers the objects. And, in testing, you can inject different objects instead (overwrite, or instantiate without using the factory method, see below)

Is there anything about the following code that raises alarm bells? Am I understanding this correctly?

abstract class AbstractClass
{
    public function __construct ()
    {

    }

    public static function factory ()
    {
        throw new Exception ('Please create a concrete class version of the method ' . __FUNCTION__);
    }

    public function inject ($class, $className=null)
    {

        if ($className === null)
        {
            $className          = get_class ($class);
        }

        $this->{$className} = $class;
    }
}

class ConcreteClass extends AbstractClass
{
    public static function factory ()
    {
        $me = new self;
        $me->inject (RebarClass::factory ());
        $me->inject (AsphaltClass::factory ());
        $me->inject (CementClass::factory ());

        return $me;
    }

    public function doSomething ()
    {
        echo $this->RebarClass->doSomethingCool ();
    }

}

class RebarClass extends AbstractClass
{
    public static function factory ()
    {
        return new self;
    }

    public function doSomethingCool ()
    {
        return "I did something, but it wasn't that cool...\n";
    }
}

class AsphaltClass extends AbstractClass
{
    public static function factory ()
    {
        return new self;
    }
}

class CementClass extends AbstractClass
{
    public static function factory ()
    {
        $me = new self;
        $me->inject (AsphaltClass::factory ());
        $me->inject (SandClass::factory ());

        return $me;
    }
}

class SandClass extends AbstractClass
{
    public static function factory ()
    {
        return new self;
    }
}

To me, this gives me a lot of flexibility when I'm creating and using objects in controllers and other models, I can instantiate like:

$concreteClass = ConcreteClass::factory ();

And now my object is set up the way I want

print_r ($concreteClass);
echo "\n";

Outputs:

ConcreteClass Object
(
    [RebarClass] => RebarClass Object
    (
    )

    [AsphaltClass] => AsphaltClass Object
    (
    )

    [CementClass] => CementClass Object
    (
        [AsphaltClass] => AsphaltClass Object
        (
        )

        [SandClass] => SandClass Object
        (
        )

    )

)

And internally the other objects are easy to use

echo $concreteClass->doSomething ();

And, if you want to use this for unit testing, you can do either:

$concreteClass = ConcreteClass::factory ();
$concreteClass->inject(new DifferentAsphaltClass, 'AsphaltClass'); // overwrite

OR

$concreteClass = new ConcreteClass; // now you are responsible for setting up dependencies yourself
$concreteClass->inject (new YetAnotherAsphaltClass, 'AsphaltClass');
Was it helpful?

Solution

The presented approach has some risks related to properly injecting the required class if not using the factory.

The code is also a little bit harder to follow due to the unavailability of the list of required injections.

I would suggest using the class constructor as the receiver of dependencies instead of using the inject method.

class Concrete {
    public static function Factory() {
        $rebar = Rebar::Factory();
        $asphalt = Asphalt::Factory();
        $sand = Sand::Factory();

        return new Concrete($rebar, $asphalt, $sand);
    }

    public function __construct(IRebar $rebar, IAsphalt $asphalt, ISand $sand) {
        $this->Rebar = $rebar;
        $this->Asphalt = $asphalt;
        $this->Sand = $sand;
    }
}

The IRebar, IAsphalt, and ISand could be interfaces ( http://php.net/manual/en/language.oop5.interfaces.php) .

interface IRebar {

}

class MyRebar implements IRebar {
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top