mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 21:17:29 +02:00
PHP7 Prototype
This commit is contained in:
@@ -9,9 +9,6 @@ class BarBookPrototype extends BookPrototype
|
|||||||
*/
|
*/
|
||||||
protected $category = 'Bar';
|
protected $category = 'Bar';
|
||||||
|
|
||||||
/**
|
|
||||||
* empty clone.
|
|
||||||
*/
|
|
||||||
public function __clone()
|
public function __clone()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Prototype;
|
namespace DesignPatterns\Creational\Prototype;
|
||||||
|
|
||||||
/**
|
|
||||||
* class BookPrototype.
|
|
||||||
*/
|
|
||||||
abstract class BookPrototype
|
abstract class BookPrototype
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -17,24 +14,13 @@ abstract class BookPrototype
|
|||||||
*/
|
*/
|
||||||
protected $category;
|
protected $category;
|
||||||
|
|
||||||
/**
|
|
||||||
* @abstract
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
abstract public function __clone();
|
abstract public function __clone();
|
||||||
|
|
||||||
/**
|
public function getTitle(): string
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getTitle()
|
|
||||||
{
|
{
|
||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $title
|
|
||||||
*/
|
|
||||||
public function setTitle($title)
|
public function setTitle($title)
|
||||||
{
|
{
|
||||||
$this->title = $title;
|
$this->title = $title;
|
||||||
|
@@ -4,11 +4,11 @@ namespace DesignPatterns\Creational\Prototype;
|
|||||||
|
|
||||||
class FooBookPrototype extends BookPrototype
|
class FooBookPrototype extends BookPrototype
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected $category = 'Foo';
|
protected $category = 'Foo';
|
||||||
|
|
||||||
/**
|
|
||||||
* empty clone.
|
|
||||||
*/
|
|
||||||
public function __clone()
|
public function __clone()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@@ -25,12 +25,6 @@ Code
|
|||||||
|
|
||||||
You can also find these code on `GitHub`_
|
You can also find these code on `GitHub`_
|
||||||
|
|
||||||
index.php
|
|
||||||
|
|
||||||
.. literalinclude:: index.php
|
|
||||||
:language: php
|
|
||||||
:linenos:
|
|
||||||
|
|
||||||
BookPrototype.php
|
BookPrototype.php
|
||||||
|
|
||||||
.. literalinclude:: BookPrototype.php
|
.. literalinclude:: BookPrototype.php
|
||||||
@@ -52,5 +46,11 @@ FooBookPrototype.php
|
|||||||
Test
|
Test
|
||||||
----
|
----
|
||||||
|
|
||||||
|
Tests/PrototypeTest.php
|
||||||
|
|
||||||
|
.. literalinclude:: Tests/PrototypeTest.php
|
||||||
|
:language: php
|
||||||
|
:linenos:
|
||||||
|
|
||||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Prototype
|
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Prototype
|
||||||
.. __: http://en.wikipedia.org/wiki/Prototype_pattern
|
.. __: http://en.wikipedia.org/wiki/Prototype_pattern
|
||||||
|
27
Creational/Prototype/Tests/PrototypeTest.php
Normal file
27
Creational/Prototype/Tests/PrototypeTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -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);
|
|
||||||
}
|
|
Reference in New Issue
Block a user