Как уже говорили другие, вы можете добавить овец в машину путем инъекции. Метод shear
на самом деле имеет для меня наибольшее значение.
public function Shear(Sheep $sheep) {
switch ($sheep->age) {} // etc.
}
На самом деле, я бы сделал еще один шаг, если бы овцы отреагировали на стрижку.
public static function makeRandom() {
// ...
return new $age ? OldSheep($color) : YoungSheep($color);
}
Или еще лучше:
private static $ages = array(0 => 'Old', 1 => 'Young')
public static function makeRandom() {
// ...
$sheepclass = $ages[$age] . 'Sheep';
return new $sheepclass($color);
}
Тогда, когда пришло время стрижки:
interface Sheepable {
function prepareToShear(machine $mach);
}
class OldSheep implements Sheepable {
public function prepareToShear(machine $mach) {
echo "Sheep is ready to be shorn";
$mach->shear($this);
}
}
class YoungSheep implements Sheepable {
public function prepareToShear(machine $mach) {
echo "Sheep is not ready to be shorn";
}
}
class machine {
//... etc.
public function Shear(Sheepable $sheep) {
$sheep->prepareToShear($this);
}
}
Кстати, просьба о помощи с домашним заданием имеет тенденцию получить здесь отрицательный ответ.