I have two types of Directors, each one is set with a different builder that creates two completely different typse of objects.
// make objects of type A
class Director_A{
function __construct($builder_A){...}
function make(){
$this->getBuilder()->step_a();
$this->getBuilder()->step_b();
}
function setBuilder($builder){ .... }
function getBuilder(){ .... }
}
// make objects of type B
class Director_B{
function __construct($builder_B){...}
function make(){
$this->getBuilder()->step_1();
$this->getBuilder()->step_2();
}
function setBuilder($builder){ .... }
function getBuilder(){ .... }
}
If I create an abstract Director class, only the make() method would vary then.
abstract class Director{
abstract make();
function setBuilder($builder){ .... }
function getBuilder(){ .... }
}
Now, here is my dilemma: should I create two Director class that extends the abstract Director with their own make() method?
class Director_A extends class Director{
function make(){
$this->getBuilder()->step_a();
$this->getBuilder()->step_b();
}
}
class Director_B extends class Director{
function make(){
$this->getBuilder()->step_1();
$this->getBuilder()->step_2();
}
}
Or should I create two different MakeBehavior concrete class that would be set in a Director object?
class Director(){
function make(){
$this->getMakeBehavior()->make();
}
}
class Director_A extends class Director{
function __construct(){
$this->setMakeBehavior(new MakeBehavior_A());
}
}
class MakeBehavior_A(){
function __construct($director){
$this->setDirector($director);
}
function make(){
$this->getDirector()->getBuilder()->step_a();
$this->getBuilder()->getBuilder()->step_b();
}
}
I feel like the second solution is the way to go because I have read multiple times to favor composition over inheritance.
However, the make() method in the MakeBehavior object violates the law of Demether, and I am stuck there.
Should I use inheritance to create a different Director class of each type of object I want to create or should I implement on Director that would use a different MakeBehavior object depending of what I want to create?
Aucun commentaire:
Enregistrer un commentaire