Created
July 4, 2019 09:41
-
-
Save vahid-almasi/5b9ec68002817ad6a3f038444612392d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface CarService | |
{ | |
public function getCost(); | |
} | |
class BasicInspection implements CarService | |
{ | |
public function getCost() | |
{ | |
return 20; | |
} | |
} | |
class OilChange implements CarService | |
{ | |
protected $car_service; | |
public function __construct(CarService $car_service) | |
{ | |
$this->car_service = $car_service; | |
} | |
public function getCost() | |
{ | |
return 19 + $this->car_service->getCost(); | |
} | |
} | |
class TireRotation implements CarService | |
{ | |
protected $car_service; | |
function __construct(CarService $car_service) | |
{ | |
$this->car_service = $car_service; | |
} | |
public function getCost() | |
{ | |
return 10 + $this->car_service->getCost(); | |
} | |
} | |
echo (new BasicInspection())->getCost(); // just basic inspection = 20 | |
echo (new TireRotation(new OilChange(new BasicInspection())))->getCost(); // basic inspection and oil change and tire rotation = 49 | |
echo (new TireRotation(new BasicInspection()))->getCost(); // basic inspection and tire rotation = 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment