I understand that having 3 or more parameters as dependency is a sign that the class may be doing too many things, however there are cases that the class really needs more than 3. My question is, is consolidating the dependencies in to just one parameter an acceptable practice?
E.g.,
Instead of Foo
needing to instantiate and pass its dependencies like this:
$dep1 = new Dep1;
$dep2 = new Dep2;
$dep3 = new Dep3;
Foo($dep1, $dep2, $dep3);
all it needs to do is something like this:
$dependencies = new foosDependencies;
Foo($dependencies);
foosDependencies
looks something like this:
class foosDependencies
{
public $dep1;
public $dep2;
public $dep3;
public function __construct()
{
$this->dep1 = new Dep1;
$this->dep2 = new Dep2;
$this->dep3 = new Dep3;
}
}
and Foo
like this:
class Foo
{
private $dependencies;
public function __construct(foosDependencies $dependencies)
{
// Foo now has access to the ff.
$dep1 = $dependencies->dep1;
$dep2 = $dependencies->dep2;
$dep3 = $dependencies->dep2;
}
}
Now if ever you need more dependencies, you just add it in foosDependencies
without the need to modify the constructor parameters of Foo
.
Is this is an acceptable method?
foosDependencies
instantiates all. I don't understand your second question.