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');