Предлагается два разных подхода.
1. Передаём в методы параметры.
Код: Выделить всё
class Dancer
{
private $clothes;
private $shoes;
private $music;
public function setScene($clothes, $shoes, $music)
{
$this->clothes = $clothes;
$this->shoes = $shoes;
$this->music = $music;
}
public function dance()
{
$this->putOnClothes($this->clothes);
$this->putOnShoes($this->shoes);
$this->turnOnMusic($this->music);
}
private function putOnClothes($clothes)
{
$clothes->putOn();
}
private function putOnShoes($shoes)
{
$shoes->putOn();
}
private function turnOnMusic($music)
{
$music->turnOn();
}
}
Код: Выделить всё
class Dancer
{
private $clothes;
private $shoes;
private $music;
public function setScene($clothes, $shoes, $music)
{
$this->clothes = $clothes;
$this->shoes = $shoes;
$this->music = $music;
}
public function dance()
{
$this->putOnClothes();
$this->putOnShoes();
$this->turnOnMusic();
}
private function putOnClothes()
{
$this->clothes->putOn();
}
private function putOnShoes()
{
$this->shoes->putOn();
}
private function turnOnMusic()
{
$this->music->turnOn();
}
}
За первый вариант:
Можно переиспользовать методы в других классах, так как они не привязаны жёстко к объекту.
За второй вариант:
Код компактнее и с виду проще.
P.S.
Тут предполагается, что в методах "putOn..." и т.д. делается какая-то более сложная работа, чем просто один вызов метода. Просто сократил для наглядности. Иначе, конечно, можно было бы всё в одной функции написать.