PHP7 Prototype

This commit is contained in:
Dominik Liebler
2016-09-22 14:56:44 +02:00
parent 61a6f03f04
commit f4c9af503b
6 changed files with 37 additions and 44 deletions

View File

@@ -9,9 +9,6 @@ class BarBookPrototype extends BookPrototype
*/
protected $category = 'Bar';
/**
* empty clone.
*/
public function __clone()
{
}

View File

@@ -2,9 +2,6 @@
namespace DesignPatterns\Creational\Prototype;
/**
* class BookPrototype.
*/
abstract class BookPrototype
{
/**
@@ -17,24 +14,13 @@ abstract class BookPrototype
*/
protected $category;
/**
* @abstract
*
* @return void
*/
abstract public function __clone();
/**
* @return string
*/
public function getTitle()
public function getTitle(): string
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;

View File

@@ -4,11 +4,11 @@ namespace DesignPatterns\Creational\Prototype;
class FooBookPrototype extends BookPrototype
{
/**
* @var string
*/
protected $category = 'Foo';
/**
* empty clone.
*/
public function __clone()
{
}

View File

@@ -25,12 +25,6 @@ Code
You can also find these code on `GitHub`_
index.php
.. literalinclude:: index.php
:language: php
:linenos:
BookPrototype.php
.. literalinclude:: BookPrototype.php
@@ -52,5 +46,11 @@ FooBookPrototype.php
Test
----
Tests/PrototypeTest.php
.. literalinclude:: Tests/PrototypeTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Prototype
.. __: http://en.wikipedia.org/wiki/Prototype_pattern

View File

@@ -0,0 +1,27 @@
<?php
namespace DesignPatterns\Creational\Pool\Tests;
use DesignPatterns\Creational\Prototype\BarBookPrototype;
use DesignPatterns\Creational\Prototype\FooBookPrototype;
class PrototypeTest extends \PHPUnit_Framework_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('DesignPatterns\Creational\Prototype\FooBookPrototype', $book);
}
for ($i = 0; $i < 5; $i++) {
$book = clone $barPrototype;
$book->setTitle('Bar Book No ' . $i);
$this->assertInstanceOf('DesignPatterns\Creational\Prototype\BarBookPrototype', $book);
}
}
}

View File

@@ -1,17 +0,0 @@
<?php
namespace DesignPatterns\Creational\Prototype;
$fooPrototype = new FooBookPrototype();
$barPrototype = new BarBookPrototype();
// now lets say we need 10,000 books of foo and 5,000 of bar ...
for ($i = 0; $i < 10000; $i++) {
$book = clone $fooPrototype;
$book->setTitle('Foo Book No '.$i);
}
for ($i = 0; $i < 5000; $i++) {
$book = clone $barPrototype;
$book->setTitle('Bar Book No '.$i);
}