Клонирование $ это для создания цепочки. Это плохая идея?

У меня следующий класс:

class StuffDoer{
public function __construct(Dep1 $dep, Dep2 $dep2, array $array){
$this->dep = $dep;
$this->dep2 = $dep2;
$this->array  = $array;
}

public function genericDoStuff($param){
// Do stuff here...
}

public function doStuffForMark(){
return $this->genericDoStuff('Mark');
}

public function doStuffForTim(){
return $this->genericDoStuff('Tim');
}

public function doStuffForAlice(){
return $this->genericDoStuff('Alice');
}
}

Через несколько месяцев меня попросили сделать метод genericDoStuff ($ param) вместе со всеми методами, которые от него зависят, использовать дополнительный параметр в одной части приложения. Вместо того, чтобы изменять сигнатуру для каждого метода, который зависит от genericDoStuff, я получил следующее:

 class StuffDoer{
public function __construct(Dep1 $dep, Dep2 $dep2, array $array){
$this->dep = $dep;
$this->dep2 = $dep2;
$this->array  = $array;
}

public function forParameter($param){
$self = clone $this;
$this->param = $param;
return $self;
}

public function genericDoStuff($param){
if($this->param !== null){
// Do stuff by taking param into account
} else {
// Do stuff stuffdoer does
}
}

public function doStuffForMark(){
return $this->genericDoStuff('Mark');
}

public function doStuffForTim(){
return $this->genericDoStuff('Tim');
}

public function doStuffForAlice(){
return $this->genericDoStuff('Alice');
}

}

Таким образом, я могу сделать это в одной точке приложения:

$myStuffDoer = $serviceContainer->get('stuff_doer');
$myStuffDoer->forParameter('AAAARGHITBURNSGODHELPME')->doStuffForMark();
// Future usages of $myStuffDoer are unaffected by this!

Поэтому мой вопрос таков: считается ли это плохой практикой по какой-либо причине?

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector