protected function __construct() { $this->automobileFactory = AutomobileFactory::getInstance(); $vipDiscount = new VipDiscount(); $this->addDiscountOption($vipDiscount); $christmasDiscount = new OrdinaryDiscount(); $this->addDiscountOption($christmasDiscount); }
<?php require_once 'Helper/AutoLoader.php'; spl_autoload_register('AutoLoader::ClassLoader'); $factory = AutomobileFactory::getInstance(); //Time::checkIfBetween("15.06.2015", "15.07.2015"); //Time::checkIfBetween("15.12.2015", "15.01.2016"); $christmas = new OrdinaryDiscount("15.06.2015", "15.07.2016", 0.15, "Reducere cu ocazia sarbatorilor de iarna"); $vip = new VipDiscount(); Helper::displayBoldMessage("Creating A-Class Vehicle"); $newAClassCar = $factory->createVehicle("a"); $newAClassCar->viewAllSpecifications(); $newAClassCar->addDiscountOption($vip); $newAClassCar->addDiscountOption($christmas); $newAClassCar->viewDiscountOptions(); Helper::displayInfoMessage("Recalculated price: " . $newAClassCar->calculatePrice()); Helper::displayBoldMessage("Equipping optional specifications..."); $newAClassCar->equipOptionalSpec("xenon"); $newAClassCar->equipOptionalSpec("volan"); $newAClassCar->equipOptionalSpec("jante"); $newAClassCar->equipOptionalSpec("cruise"); $newAClassCar->viewOptionalSpecs(); Helper::displayInfoMessage("Recalculated price: " . $newAClassCar->calculatePrice()); $newAClassCar->deleteSpec("xenon"); Helper::displayInfoMessage("Recalculated price: " . $newAClassCar->calculatePrice()); $newAClassCar->deleteSpec("volan"); Helper::displayInfoMessage("Recalculated price: " . $newAClassCar->calculatePrice()); $newAClassCar->deleteSpec("xenon"); Helper::displayInfoMessage("Recalculated price: " . $newAClassCar->calculatePrice()); //$newBClassCar = $factory->createVehicle("b"); //$newBClassCar->viewAllSpecifications();
<?php class Automobile { private $vehichleMake; private $vehicleModel; public function __construct($make, $model) { $this->vehichleMake = $make; $this->vehicleModel = $model; } public function getMakeAndModel() { return $this->vehichleMake . ' ' . $this->vehicleModel; } } class AutomobileFactory { public static function create($make, $model) { return new Automobile($make, $model); } } // have the factory create the Automobile object $veyron = AutomobileFactory::create('Bugatti', 'Veyron'); print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"
<?php require 'Automobile.php'; require 'AutomobileFactory.php'; $citroen = AutomobileFactory::create('Citroen', 'C5'); print_r($citroen->get_make_and_model());
<?php // Problem // A framework needs to standardize the architectural model for a range of applications, // but allow for individual applications to define their own domain objects // and provide for their instantiation. class Automobile { private $vehicleMake; private $vehicleModel; public function __construct($make, $model) { $this->vehicleMake = $make; $this->vehicleModel = $model; } public function getMakeAndModel() { return $this->vehicleMake . ' ' . $this->vehicleModel; } } class AutomobileFactory { public static function create($make, $model) { return new Automobile($make, $model); } } // have the factory create the Automobile object $Aventador = AutomobileFactory::create('Lomborghini', 'Aventador'); print_r($Aventador->getMakeAndModel()); // outputs "Lomborghinni Aventador"