Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f78379

Browse files
merged from master
2 parents d225a43 + ed291cd commit 2f78379

File tree

14 files changed

+241
-31
lines changed

14 files changed

+241
-31
lines changed

Behavioral/Strategy/DateComparator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class DateComparator implements ComparatorInterface
1212
*/
1313
public function compare($a, $b)
1414
{
15-
$aDate = strtotime($a['date']);
16-
$bDate = strtotime($b['date']);
15+
$aDate = new \DateTime($a['date']);
16+
$bDate = new \DateTime($b['date']);
1717

1818
if ($aDate == $bDate) {
1919
return 0;

Behavioral/Strategy/index.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

Behavioral/TemplateMethod/Journey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private function buyAFlight()
4545
}
4646

4747
/**
48-
* sbclasses will get access to this method but cannot override it and
48+
* Subclasses will get access to this method but cannot override it and
4949
* compromise this algorithm (warning : cause of cyclic dependencies)
5050
*/
5151
final protected function takePlane()

Creational/Singleton/Singleton.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Singleton
1010
/**
1111
* @var cached reference to singleton instance
1212
*/
13-
protected static $instance;
13+
private static $instance;
1414

1515
/**
1616
* gets the instance via lazy initialization (created on first usage)

Pool/Pool.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace DesignPatterns\Pool;
4+
5+
class Pool
6+
{
7+
8+
private $instances = array();
9+
private $class;
10+
11+
public function __construct($class)
12+
{
13+
$this->class = $class;
14+
}
15+
16+
public function get()
17+
{
18+
if (count($this->instances) > 0) {
19+
return array_pop($this->instances);
20+
}
21+
22+
return new $this->class();
23+
}
24+
25+
public function dispose($instance)
26+
{
27+
$this->instances[] = $instance;
28+
}
29+
30+
}

Pool/Processor.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace DesignPatterns\Pool;
4+
5+
class Processor
6+
{
7+
8+
private $pool;
9+
private $processing = 0;
10+
private $maxProcesses = 3;
11+
private $waitingQueue = [];
12+
13+
public function __construct(Pool $pool)
14+
{
15+
$this->pool = $pool;
16+
}
17+
18+
public function process($image)
19+
{
20+
if ($this->processing++ < $this->maxProcesses) {
21+
$this->createWorker($image);
22+
} else {
23+
$this->pushToWaitingQueue($worker);
24+
}
25+
}
26+
27+
private function createWorker($image)
28+
{
29+
$worker = $this->pool->get();
30+
$worker->run($image, array($this, 'processDone'));
31+
}
32+
33+
public function processDone($worker)
34+
{
35+
$this->processing--;
36+
$this->pool->dispose($worker);
37+
38+
if (count($this->waitingQueue) > 0) {
39+
$this->createWorker($this->popFromWaitingQueue());
40+
}
41+
}
42+
43+
private function pushToWaitingQueue($image)
44+
{
45+
$this->waitingQueue[] = $image;
46+
}
47+
48+
private function popFromWaitingQueue()
49+
{
50+
return array_pop($this->waitingQueue);
51+
}
52+
53+
}

Pool/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Pool
2+
====
3+
4+
The **object pool pattern** is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object, which is a specific type of factory object, to the pool rather than destroying it.
5+
6+
Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low. The pooled object is obtained in predictable time when creation of the new objects (especially over network) may take variable time.
7+
8+
However these benefits are mostly true for objects that are expensive with respect to time, such as database connections, socket connections, threads and large graphic objects like fonts or bitmaps. In certain situations, simple object pooling (that hold no external resources, but only occupy memory) may not be efficient and could decrease performance.

Pool/Worker.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DesignPatterns\Pool;
4+
5+
class Worker
6+
{
7+
8+
public function __construct()
9+
{
10+
// let's say that constuctor does really expensive work...
11+
// for example creates "thread"
12+
}
13+
14+
public function run($image, array $callback)
15+
{
16+
// do something with $image...
17+
// and when it's done, execute callback
18+
call_user_func($callback, $this);
19+
}
20+
21+
}

Pool/index.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use DesignPatterns\Pool\Pool;
4+
use DesignPatterns\Pool\Processor;
5+
6+
$pool = new Pool('DesignPatterns\Pool\Worker');
7+
$processor = new Processor($pool);
8+
9+
$processor->process('image1.jpg');
10+
$processor->process('image2.jpg');
11+
$processor->process('image3.jpg');
12+
$processor->process('image4.jpg');
13+
$processor->process('image5.jpg');
14+
$processor->process('image6.jpg');
15+
$processor->process('image7.jpg');
16+
$processor->process('image8.jpg');

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ I think the problem with patterns is that often people do know them but don't kn
88

99
## Patterns
1010

11-
The patterns can be structured in roughly three different categories. Please click on the :notebook: for a full explanation of the pattern on Wikipedia.
11+
The patterns can be structured in roughly three different categories. Please click on the [:notebook:](http://en.wikipedia.org/wiki/Software_design_pattern) for a full explanation of the pattern on Wikipedia.
1212

1313
### Creational
1414

@@ -18,6 +18,7 @@ The patterns can be structured in roughly three different categories. Please cli
1818
* [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern)
1919
* [StaticFactory](StaticFactory)
2020
* [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern)
21+
* [Pool](Pool) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern)
2122
* [Singleton](Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:)
2223
* [Multiton](Multiton) (is considered an anti-pattern! :no_entry:)
2324

Structural/Composite/README.md

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
# Purpose
44

5-
To treat a group of objects the same way as a single instance of the object.
5+
To treat a group of objects the same way as a single instance of the object.
66

77
# Examples
88

99
* a form class instance handles all its form elements like a single instance of the form, when `render()` is called, it
10-
subsequently runs trough all its child elements and calls `render()` on them
10+
subsequently runs through all its child elements and calls `render()` on them
1111
* `Zend_Config`: a tree of configuration options, each one is a `Zend_Config` object itself
1212

Structural/DependencyInjection/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ To implement a loosely coupled architecture in order to get better testable, mai
66

77
## Usage
88

9-
Configuration gets injected and `Connection` will get all that it needs from Configuration Without DI, the configuration would be created directly in Connection, which is not very good for testing and extending `Connection`.
9+
Configuration gets injected and `Connection` will get all that it needs from `$config`. Without DI, the configuration would be created directly in `Connection`, which is not very good for testing and extending `Connection`.
1010

11-
Notice we are following Inversion of control principle in `Connection` by asking `$config` to implement `Parameters` interface. This decouples our components. We don't care where the source of information comes from, we only care that config has certain methods to retrieve that information. Read more about Inversion of control [here](http://en.wikipedia.org/wiki/Inversion_of_control).
11+
Notice we are following Inversion of control principle in `Connection` by asking `$config` to implement `Parameters` interface. This decouples our components. We don't care where the source of information comes from, we only care that `$config` has certain methods to retrieve that information. Read more about Inversion of control [here](http://en.wikipedia.org/wiki/Inversion_of_control).
1212

1313
## Examples
1414

15-
* the Doctrine2 ORM uses dependency injection e.g. for Configuration that is injected into a Connection object. for testing purposes, one can easily create a mock object of the configuration and inject that into the connection object
15+
* The Doctrine2 ORM uses dependency injection e.g. for configuration that is injected into a `Connection` object. For testing purposes, one can easily create a mock object of the configuration and inject that into the `Connection` object
1616
* Symfony and Zend Framework 2 already have containers for DI that create objects via a configuration array and inject them where needed (i.e. in Controllers)

Tests/Pool/PoolTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DesignPatterns\Tests\Pool;
4+
5+
use DesignPatterns\Pool\Pool;
6+
7+
class TestWorker
8+
{
9+
10+
public $id = 1;
11+
12+
}
13+
14+
class PoolTest extends \PHPUnit_Framework_TestCase
15+
{
16+
17+
public function testPool()
18+
{
19+
$pool = new Pool('DesignPatterns\Tests\Pool\TestWorker');
20+
$worker = $pool->get();
21+
22+
$this->assertEquals(1, $worker->id);
23+
24+
$worker->id = 5;
25+
$pool->dispose($worker);
26+
27+
$this->assertEquals(5, $pool->get()->id);
28+
$this->assertEquals(1, $pool->get()->id);
29+
}
30+
31+
}
32+

Tests/Strategy/StrategyTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace DesignPatterns\Tests\Strategy;
4+
5+
use DesignPatterns\Strategy\DateComparator;
6+
use DesignPatterns\Strategy\IdComparator;
7+
use DesignPatterns\Strategy\ObjectCollection;
8+
use DesignPatterns\Strategy\Strategy;
9+
10+
/**
11+
* Tests for Static Factory pattern
12+
13+
*/
14+
class StrategyTest extends \PHPUnit_Framework_TestCase
15+
{
16+
17+
public function getIdCollection()
18+
{
19+
return array(
20+
array(
21+
array(array('id' => 2), array('id' => 1), array('id' => 3)),
22+
array('id' => 1)
23+
),
24+
array(
25+
array(array('id' => 3), array('id' => 2), array('id' => 1)),
26+
array('id' => 1)
27+
),
28+
);
29+
}
30+
31+
public function getDateCollection()
32+
{
33+
return array(
34+
array(
35+
array(array('date' => '2014-03-03'), array('date' => '2015-03-02'), array('date' => '2013-03-01')),
36+
array('date' => '2013-03-01')
37+
),
38+
array(
39+
array(array('date' => '2014-02-03'), array('date' => '2013-02-01'), array('date' => '2015-02-02')),
40+
array('date' => '2013-02-01')
41+
),
42+
);
43+
}
44+
45+
/**
46+
* @dataProvider getIdCollection
47+
*/
48+
public function testIdComparator($collection, $expected)
49+
{
50+
$obj = new ObjectCollection($collection);
51+
$obj->setComparator(new IdComparator());
52+
$elements = $obj->sort();
53+
54+
$firstElement = array_shift($elements);
55+
$this->assertEquals($expected, $firstElement);
56+
}
57+
58+
/**
59+
* @dataProvider getDateCollection
60+
*/
61+
public function testDateComparator($collection, $expected)
62+
{
63+
$obj = new ObjectCollection($collection);
64+
$obj->setComparator(new DateComparator());
65+
$elements = $obj->sort();
66+
67+
$firstElement = array_shift($elements);
68+
$this->assertEquals($expected, $firstElement);
69+
}
70+
}

0 commit comments

Comments
 (0)