mardi 26 avril 2016

Tight coupling & DI threshold

I have a code that goes like

class A
{
    public $b, $c;

    //Inject D and E here? (B $b, C $c, D $d, E $e, ...)
    public function __construct(B $b, C $c)
    {
        $this->b = $b;
        $this->c = $c;
    }

    public function main()
    {
        if (rand(0,1)) {
            new D();
        } else {
            new E();
        }
    }
}

Should I inject the possible D and E classes in constructor of A? The problem here is that the hierarchy of the project follows this tree-like structure thus making the upper node A to hold all the dependencies of its child nodes. Or is it sane enough to be stupid at some point?

Aucun commentaire:

Enregistrer un commentaire