First of all sorry for not explaining this correctly I have hard time finding my words.
I been following a laracast php tutorial about The Decorator Pattern. It explain how you can build up an object at run time.
From the tutorial
interface CarService{
public function getCost();
}
class Service implements CarService {
public function getCost()
{
return 20;
}
}
class OilChange implements CarService{
protected $carService;
public function __construct(CarService $carService)
{
$this->carService = $carService;
}
public function getCost()
{
return 10 + $this->carService->getCost();
}
}
class Tyre implements CarService {
protected $carService;
public function __construct(CarService $carService)
{
$this->carService = $carService;
}
public function getCost()
{
return 20 + $this->carService->getCost();
}
}
The main bit im focus on is this
$carService = new Tyre(new OilChange(new Service()));
echo $carService->getCost();
//output 50
As the tutorial describes having this code helps minimize repetition of code in the classes by introducing an interface. I understand why its a good approach but there is a bit that wasnt explained in the tutorial. if you have 100 types of services there is a possibility of 10,000 different class initiation such as this one $carService = new Tyre(new OilChange(new Service()));
or this one $carService = new OilChange(new Service());
As you can see this will quickly add up if you have to initiate classes base on each possible outcome of car service (10,000).
how could I solve this so that it knows exactly what class/type of service to initialize? clearly having a long if statement for every single condition which would be 10,000 is not correct.