コード例 #1
0
    }
    public function getCost()
    {
        return 10 + $this->carService->getCost();
    }
    public function getDescription()
    {
        return "oil change car service" . " and " . $this->carService->getDescription();
    }
}
class TireRotation implements CarService
{
    protected $carService;
    public function __construct(CarService $carService)
    {
        $this->carService = $carService;
    }
    public function getCost()
    {
        return 5 + $this->carService->getCost();
    }
    public function getDescription()
    {
        return "tire change car service" . " and " . $this->carService->getDescription();
    }
}
//$object = new BaseCarService();
//$object = new OilChange(new BaseCarService());
$object = new TireRotation(new OilChange(new BaseCarService()));
echo $object->getDescription() . "\n";
echo $object->getCost() . "\n";
コード例 #2
0
        return 29 + $this->carService->getCost();
    }
    public function getDescription()
    {
        return $this->carService->getDescription() . ", and oil change";
    }
}
class TireRotation implements CarService
{
    /**
     * @var CarService
     */
    private $carService;
    function __construct(CarService $carService = null)
    {
        $this->carService = $carService;
    }
    public function getCost()
    {
        return 15 + $this->carService->getCost();
    }
    public function getDescription()
    {
        return $this->carService->getDescription() . ", and tire rotation";
    }
}
$service = new TireRotation(new OilChange(new BasicInspections()));
echo $service->getCost();
echo PHP_EOL;
echo $service->getDescription();
echo PHP_EOL;