I have a class which depends on 3 classes, all 3 of which have other classes they rely on. Currently, I'm using a container class to build up all the required classes, inject them into one another and return the application.
The simplified version of the container looks something like this:
class Builder
{
private $_options;
public function __construct($options)
{
$this->_options = $options;
}
public function build()
{
$cache = $this->getCache();
$response = $this->getResponse();
$engine = $this->getEngine();
return new Application($cache,$response,$engine);
}
public function getResponse()
{
$encoder = $this->getResponseEncoder();
$cache = $this->getResponseCache();
return new Response($encoder,$cache);
}
// Methods for building each object
}
I'm not sure if this would be classified as FactoryMethod or a DI Container. They both seem to solve the same problem in the same way - They build objects and inject dependencies. This container has some more complicated building methods, like loading observers and attaching them to observable objects.
Should factories be doing all the building (loading extensions etc) and the DI container should use these factories to inject dependencies? That way the sub-packages, like Cache, Response etc, can each have their own specialised factories.