Example #1
0
    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();
Example #2
0
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'];
Example #3
0
    <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>
Example #4
0
<?php

function __autoload($class)
{
    include $class . '.php';
}
$factory = new ShapeFactory();
$circle = $factory->create("circle", 4);
var_dump($circle);
Example #5
0
    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>
Example #6
0
<?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();
Example #7
0
{
    // 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();
Example #8
0
<?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();
Example #9
0
/**
 * 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);
    }
}
Example #10
0
<?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);