1.6. Prototype
1.6.1. Purpose
To avoid the cost of creating objects the standard way (new Foo()) andinstead create a prototype and clone it.
1.6.2. Examples
- Large amounts of data (e.g. create 1,000,000 rows in a database atonce via a ORM).
1.6.3. UML Diagram
1.6.4. Code
You can also find this code on GitHub
BookPrototype.php
- <?php
- namespace DesignPatterns\Creational\Prototype;
- abstract class BookPrototype
- {
- /**
- * @var string
- */
- protected $title;
- /**
- * @var string
- */
- protected $category;
- abstract public function __clone();
- public function getTitle(): string
- {
- return $this->title;
- }
- public function setTitle($title)
- {
- $this->title = $title;
- }
- }
BarBookPrototype.php
- <?php
- namespace DesignPatterns\Creational\Prototype;
- class BarBookPrototype extends BookPrototype
- {
- /**
- * @var string
- */
- protected $category = 'Bar';
- public function __clone()
- {
- }
- }
FooBookPrototype.php
- <?php
- namespace DesignPatterns\Creational\Prototype;
- class FooBookPrototype extends BookPrototype
- {
- /**
- * @var string
- */
- protected $category = 'Foo';
- public function __clone()
- {
- }
- }
1.6.5. Test
Tests/PrototypeTest.php
- <?php
- namespace DesignPatterns\Creational\Prototype\Tests;
- use DesignPatterns\Creational\Prototype\BarBookPrototype;
- use DesignPatterns\Creational\Prototype\FooBookPrototype;
- use PHPUnit\Framework\TestCase;
- class PrototypeTest extends TestCase
- {
- public function testCanGetFooBook()
- {
- $fooPrototype = new FooBookPrototype();
- $barPrototype = new BarBookPrototype();
- for ($i = 0; $i < 10; $i++) {
- $book = clone $fooPrototype;
- $book->setTitle('Foo Book No ' . $i);
- $this->assertInstanceOf(FooBookPrototype::class, $book);
- }
- for ($i = 0; $i < 5; $i++) {
- $book = clone $barPrototype;
- $book->setTitle('Bar Book No ' . $i);
- $this->assertInstanceOf(BarBookPrototype::class, $book);
- }
- }
- }