public function __construct() { $carFactory = new CarFactory(); $shipFactory = new ShipFactory(); $smallCar = $carFactory->createSmallVehicle(); $bigCar = $carFactory->createBigVehicle(); $smallShip = $shipFactory->createSmallVehicle(); $bigShip = $shipFactory->createBigVehicle(); }
<?php require 'cart.php'; $cart = array(CarFactory::create('Bmw'), CarFactory::create('xyzwtf')); foreach ($cart as $car) { $totalPrice += $car->getPrice(); } echo "The total price for all the cars found in Cart is: {$totalPrice}";
public static function getCar($make, $model) { // Remember how to make the first character uppercase $className = ucfirst(strtolower($model)); //echo '$className=' . $className; // How do you make sure a class exists? if (!class_exists($className)) { // How do you throw exceptions? throw new Exception($className . ' does not exist!'); } // Remember how to dynamically instantiate objects $obj = new $className(); $make = ucfirst($make); // How do you make sure the parent class is valid? if (!is_subclass_of($obj, $make)) { throw new Exception($className . ' is not a subclass of ' . $make); } return $obj; } } class Toyota { } class Corolla extends Toyota { } class Prius extends Toyota { } $car = CarFactory::getCar('toyota', 'corolla'); print_r($car);
} class ToyotaCorolla extends Car { } class ToyotaCamry extends Car { } class ToyotaCrx extends Car { } class HondaCivic extends Car { } class CarFactory { public static function getCar($make, $model, $color) { $make = ucfirst($make); $model = ucfirst($model); $className = $make . $model; if (!class_exists($className)) { throw new Exception($className . ' does not exist!'); } $classObj = new $className($make, $model, $color); return $classObj; } } // ----------------------------------------------------------- $carObject = CarFactory::getCar('toyota', 'crx', 'black'); echo '<pre>'; print_r($carObject);
{ $model = ucfirst(strtolower($model)); if (!class_exists($model)) { throw new Exception($model . ' is not a valid model'); } $newCar = new $model(); $parent = get_parent_class($newCar); $make = ucfirst(strtolower($make)); //can also use is_subclass_of($child, $parent) if ($parent != $make) { throw new Exception($make . ' is not a valid make for this model'); } return $newCar; } } class Toyota { } class Corolla extends Toyota { } class Prius extends Toyota { } try { $prius = CarFactory::getCar('Honda', 'Prius'); echo '<pre>'; print_r($prius); } catch (Exception $e) { echo $e->getMessage(); }
class CarFactory { public static function getCar($make, $model) { if ($make == 'corolla') { return new Corolla($model); } elseif ($make == 'camry') { return new Camry($model); } else { throw new NotImplementedException($make . ' is not defined!'); } } } try { $corolla = CarFactory::getCar('corolla', 'DX'); $camry = CarFactory::getCar('camry', 'LX'); echo 'Corollas Torque: ' . $corolla->getTorque() . '<br/>'; echo 'Camrys Torque: ' . $camry->getTorque() . '<br/>'; } catch (NotImplementedException $exceptionObject) { echo '<p style="color:red;">' . $exceptionObject->getMessage() . '</p>'; } catch (DeveloperException $devException) { // Something caught on fire } catch (GooglePaymentFailedException $googleException) { // Send out SMS alerts, ring the phones, dim the lights } class UserException extends Exception { } class DeveloperException extends Exception { }
echo '<pre>'; print_r($carObject); $parentClass = get_parent_class($carObject); // you will need to instantiate this prior if (ucfirst($make) != $parentClass) { throw new Exception($make . ' is not valid for ' . $model); } return $carObject; // print_r('$parentClass= ' . $parentClass); // echo '$className= ' . $className . '</br>'; } } abstract class Toyota { } class Corolla extends Toyota { } class Camry extends Toyota { } class Prius extends Toyota { } try { $car = CarFactory::getCar('honda', 'corolla'); echo '<pre>'; print_r($car); } catch (Exception $e) { echo '<font color="red">' . $e->getMessage() . '</font>'; }
<?php require_once 'imports.php'; // I want an economical car, coloured blue... $car_factory = new CarFactory(); $car = $car_factory->build(VehicleFactory::ECONOMICAL, Vehicle::BLUE); // I am a "white van man"... $van_factory = new VANFactory(); $van = $van_factory->build(VehicleFactory::POWERFUL, Vehicle::WHITE); // Create a red sports car $sporty = VehicleFactory::make(VehicleFactory::CAR, VehicleFactory::POWERFUL, Vehicle::RED); ?> <html> <body> <p><?php echo $car; ?> </p> <p><?php echo $van; ?> </p> <p><?php echo $sporty; ?> </p> </body> </html>
if (!class_exists($className)) { throw new Exception($className . ' has not been created!'); } // Instantiate the class dynamically $carObject = new $className(); $parentClass = get_parent_class($carObject); if (ucfirst($make) != $parentClass) { throw new Exception($make . ' is not valid for ' . $model); } return $carObject; } } abstract class Toyota { } class Corolla extends Toyota { } class Camry extends Toyota { } class Prius extends Toyota { } try { $car = CarFactory::getCar('toyota', 'civic'); echo '<pre>'; print_r($car); } catch (Exception $whatevs) { echo '<font color="red">' . $whatevs->getMessage() . '</font>'; }
class Aveo implements Conveyor { public function buildEngine() { echo "<p>Aveo engine built</p>"; } public function attachWheels() { echo "<p>Aveo wheels attached</p>"; } public function testing() { echo "<p>Aveo tested</p>"; } } class CarFactory { public static function factory($car) { $className = $car; $object = new $className(); return $object; } } $autoList = ['lanos', 'lanos', 'aveo']; foreach ($autoList as $auto) { $obj = CarFactory::factory($auto); $obj->buildEngine(); $obj->attachWheels(); $obj->testing(); }
<?php header('Content-Type:text/html;charset=utf-8'); /* * 简单工厂设计模式【买车】 * Author: Kaysen */ define('ROOT_PATH', dirname(__FILE__)); require_once ROOT_PATH . '/../../Loader.php'; $test = CarFactory::BuyCar('Audi', 'Comfort'); var_dump($test);
public function testFactoryReturnsAnObject() { $car = CarFactory::create('carThatDoesNotExist'); $this->assertEquals(0, $car->getPrice()); }