0

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.

Luke
  • 146
  • 13
  • 1
    I'm far from being an OOP expert, but to be honest this example seems to be more like the "composite pattern". All of your classes basically perform the same operation, of reporting their price. You're constructing a "tree", that reports its overall price, by calling the getCost method of every "leaf". Take a look here: https://stackoverflow.com/questions/2233952/difference-between-the-composite-pattern-and-decorator-pattern – i.brod Mar 05 '20 at 13:03
  • 1
    As to your question, i do not know how to avoid the "if else", Because this example is extremely impractical. One tip: OOP design patterns can be abused. In real life, you will probably not use a design, where you have 10000 possible compositions, that need to be decided during run time. I'd suggest to take every example you're given with caution, and treat it just as a pedagogy tool. – i.brod Mar 05 '20 at 13:06
  • Thanks I will have a look at the url you provided :) – Luke Mar 05 '20 at 13:13
  • I think the key feature of decorator pattern, is to "decorate" some object with additional functionality, unlike in the provided example. – i.brod Mar 05 '20 at 18:09

0 Answers0