
<?php
//Prototype
class Shape {
public $lineCount = 0;
function __construct(int $lineCount) {
$this->lineCount = $lineCount;
}
function clone(): Shape {
return clone $this;
}
}
//ConcretePrototype
class Square extends Shape {
function __construct() {
parent::__construct(4);
}
}
//Client
class ShapeMaker {
private $shape;
function __construct(Shape $shape) {
$this->shape = $shape;
}
function makeShape() {
return $this->shape->clone();
}
}
$square = new Square();
$maker = new ShapeMaker($square);
$square1 = $maker->makeShape();
$square2 = $maker->makeShape();
echo $square1->lineCount, "\n";
echo $square1 === $square2 ? "true" : "false";
| Specify the kinds of objects to create using a prototypical instance, and create new objectsby copying this prototype. |