public function draw() { echo 'demo factory'; } } class Monkey { public function monney() { echo 'demo'; } } class ShapeFactory { public function create($type) { if ($type == 'Rectangle') { return new Rectangle(new Position()); } elseif ($type == 'Monkey') { return new Monkey(); } } } $factory = new ShapeFactory(); //$rect =$factory->create('Rectangle'); $rect = $factory->create('Monkey'); //$rect->draw(); $rect->monney(); echo "<pre>"; var_dump($rect); //$rect->draw();
spl_autoload_register(function ($class) { require $class . '.php'; }); // Minimal validation: if (isset($_GET['submit'])) { if (isset($_GET['shape'], $_GET['dim'])) { $dim = trim($_GET['dim']); if (empty($dim)) { die('<p class="error">Please provide dimensions in correct format.</p>'); } $dim = explode(',', $dim); var_dump($dim); $dim = array_map('intval', $dim); var_dump($dim); // Create the new object: $obj = ShapeFactory::Create($_GET['shape'], $dim); // Print a little introduction: echo "<h2>Creating a {$_GET['shape']}...</h2>"; // Print the area: echo '<p>The area is ' . $obj->getArea() . '</p>'; // Print the perimeter: echo '<p>The perimeter is ' . $obj->getPerimeter() . '</p>'; } else { echo '<p class="error">Please provide a shape type and size.</p>'; } // Delete the object: unset($obj); } ?> <form action="<?php echo $_SERVER['PHP_SELF'];
<meta charset="utf-8"> <title>Factory</title> <link rel="stylesheet" href="style.css"> </head> <body> <?php # Script 7.4 - factory.php // This page uses the ShapeFactory class (Script 7.2). // Load the class definitions: require 'ShapeFactory.php'; require 'Shape.php'; require 'Triangle.php'; require 'Rectangle.php'; // Minimal validation: if (isset($_GET['shape'], $_GET['dimensions'])) { // Create the new object: $obj = ShapeFactory::Create($_GET['shape'], $_GET['dimensions']); // Print a little introduction: echo "<h2>Creating a {$_GET['shape']}...</h2>"; // Print the area: echo '<p>The area is ' . $obj->getArea() . '</p>'; // Print the perimeter: echo '<p>The perimeter is ' . $obj->getPerimeter() . '</p>'; } else { echo '<p class="error">Please provide a shape type and size.</p>'; } // Delete the object: unset($obj); ?> </body> </html>
<?php function __autoload($class) { include $class . '.php'; } $factory = new ShapeFactory(); $circle = $factory->create("circle", 4); var_dump($circle);
This will give the result of area = 168 and perimeter = 52 --------------------------------------- Triangle Test: factory.php?shape=triangle&dimensions[]=12&dimensions[]=14&dimensions[]=5 This will give the result of area = 29.230762904858 and perimeter = 31 --------------------------------------- Shapes not exist Test: factory.php?shape=tfriangle&dimensions[]=12&dimensions[]=14&dimensions[]=5 --> <?php require 'ShapeFactory.php'; require 'Shape.php'; require 'Rectangle.php'; require 'Triangle.php'; //perform minimal validation if (isset($_GET['shape'], $_GET['dimensions'])) { //create the object $shape = $_GET['shape']; $dimensions = $_GET['dimensions']; $obj = ShapeFactory::Create($shape, $dimensions); echo "<h2> Creating a {$shape} ...</h2>"; /* As $obj is now some sort of Shape-based object, you know it has a getArea() method that can be called. */ echo "<p>The area is " . $obj->getArea() . "</p>"; echo "<p>The parameter is " . $obj->getPerimeter() . "</p>"; } else { echo "<p>Please provide a shape type and size.</p>"; } unset($obj, $shape, $dimensions); ?> </body> </html>
<?php function __autoload($class_name) { include 'src/' . $class_name . '.php'; } // name of render to use from POST $render = isset($_POST['render']) ? $_POST['render'] : "RenderImage"; // shapes to draw with params from POST $shapes = isset($_POST['shapes']) ? $_POST['shapes'] : [['type' => 'circle', 'params' => ['width' => 1, 'color' => 'red']], ['type' => 'square', 'params' => ['width' => 0.5, 'color' => 'blue']]]; $View = new View($render); foreach ($shapes as $shape_data) { $shape = ShapeFactory::build($shape_data['type'], $shape_data['params']); $View->add($shape); } $View->render();
{ // use getShape method to get object of type Shape public function getShape($shapeType) { if (empty($shapeType)) { return NULL; } if (strtoupper($shapeType) == "CIRCLE") { return new Circle(); } else { if (strtoupper($shapeType) == "RECTANGLE") { return new Rectangle(); } else { if (strtoupper($shapeType) == "SQUARE") { return new Square(); } } } return NULL; } } $shapeFactory = new ShapeFactory(); // get an object of type Circle and call its draw method. $shape1 = $shapeFactory->getShape("CIRCLE"); $shape1->draw(); // get an object of type Rectangle and call its draw method. $shape2 = $shapeFactory->getShape("RECTANGLE"); $shape2->draw(); // get an object of type Square and call its draw method. $shape3 = $shapeFactory->getShape("SQUARE"); $shape3->draw();
<?php require_once 'ShapeFactory.php'; require_once 'Shape.php'; require_once 'Square.php'; require_once 'Circle.php'; require_once 'Rectangle.php'; $shapeFactory = new ShapeFactory(); $circle = $shapeFactory->getShape('CIRCLE'); $circle->draw(); $square = $shapeFactory->getShape('SQUARE'); $square->draw(); $rectangle = $shapeFactory->getShape('RECTANGLE'); $rectangle->draw();
/** * Reads from a file * Using the Quicksort algorithm * @param $file the path to a file $shapes An array for storing a collection of shapes */ function createShapeArray($file, &$shapes) { $filedata = file_get_contents($file, true); $filearray = explode("\n", $filedata); // Get rid of first line $headers = array_shift($filearray); $wordarray = array(); $filearray = array_filter($filearray); foreach ($filearray as $filetext) { $shape_array = explode(',', $filetext); $shapes[] = ShapeFactory::createShape($shape_array); } }
<?php abstract class ShapeFactory { static function Create($type, array $size) { switch ($type) { case 'rect': return new rect($size[0], $size[1]); break; case 'tri': return new tri($size[0], $size[1], $size[2]); break; } } } $dimension = array(); $dimension[] = 1; $dimension[] = 2; $obj = ShapeFactory::Create('tri', $dimension);