From 678a5b74a9a6faf8fc90d65c33911e5002e6154e Mon Sep 17 00:00:00 2001 From: Lester Pena Date: Sun, 9 Mar 2014 13:23:43 -0300 Subject: [PATCH 01/12] added link to explanation of the pattern on Wikipedia --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b34051e..e7675c6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ I think the problem with patterns is that often people do know them but don't kn ## Patterns -The patterns can be structured in roughly three different categories. Please click on the :notebook: for a full explanation of the pattern on Wikipedia. +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. ### Creational From 11d437c7cdf5ac91dd32b1942087f2e8b533f9fe Mon Sep 17 00:00:00 2001 From: Lester Pena Date: Sun, 9 Mar 2014 13:25:33 -0300 Subject: [PATCH 02/12] added link to explanation of the pattern on Wikipedia --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e7675c6..c5a6415 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ I think the problem with patterns is that often people do know them but don't kn ## Patterns -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. +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. ### Creational From f815f664361de83107c888d7b194477b3e22c9c4 Mon Sep 17 00:00:00 2001 From: Salvatore Cozzolongo Date: Wed, 12 Mar 2014 11:29:33 +0100 Subject: [PATCH 03/12] private on attribute --- Singleton/Singleton.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Singleton/Singleton.php b/Singleton/Singleton.php index ce7ee25..0aee908 100644 --- a/Singleton/Singleton.php +++ b/Singleton/Singleton.php @@ -10,7 +10,7 @@ class Singleton /** * @var cached reference to singleton instance */ - protected static $instance; + private static $instance; /** * gets the instance via lazy initialization (created on first usage) From 2b874be525c05510875f58247469bae1f6ca8339 Mon Sep 17 00:00:00 2001 From: bosoniq Date: Fri, 21 Mar 2014 13:26:55 +0100 Subject: [PATCH 04/12] Fixed misspelling of through modified: Composite/README.md --- Composite/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 Composite/README.md diff --git a/Composite/README.md b/Composite/README.md old mode 100644 new mode 100755 index 01732d1..6d4cea3 --- a/Composite/README.md +++ b/Composite/README.md @@ -7,6 +7,6 @@ To treat a group of objects the same way as a single instance of the object. # Examples * a form class instance handles all its form elements like a single instance of the form, when `render()` is called, it - subsequently runs trough all its child elements and calls `render()` on them + subsequently runs through all its child elements and calls `render()` on them * `Zend_Config`: a tree of configuration options, each one is a `Zend_Config` object itself From 71d894e10343ea73e970c2717dfbd3890ab99cc1 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Fri, 21 Mar 2014 14:21:23 -0300 Subject: [PATCH 05/12] creating a test to Strategy and DateComparator refactored --- Strategy/DateComparator.php | 4 +- Strategy/index.php | 21 ---------- Tests/Strategy/StrategyTest.php | 70 +++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 23 deletions(-) delete mode 100644 Strategy/index.php create mode 100644 Tests/Strategy/StrategyTest.php diff --git a/Strategy/DateComparator.php b/Strategy/DateComparator.php index 9025acc..199716b 100644 --- a/Strategy/DateComparator.php +++ b/Strategy/DateComparator.php @@ -12,8 +12,8 @@ class DateComparator implements ComparatorInterface */ public function compare($a, $b) { - $aDate = strtotime($a['date']); - $bDate = strtotime($b['date']); + $aDate = new \DateTime($a['date']); + $bDate = new \DateTime($b['date']); if ($aDate == $bDate) { return 0; diff --git a/Strategy/index.php b/Strategy/index.php deleted file mode 100644 index d04caa2..0000000 --- a/Strategy/index.php +++ /dev/null @@ -1,21 +0,0 @@ - 2, - 'date' => '2011-01-01', - ), - array( - 'id' => 1, - 'date' => '2011-02-01' - ) -); - -$collection = new ObjectCollection($elements); -$collection->setComparator(new IdComparator()); -$collection->sort(); - -$collection->setComparator(new DateComparator()); -$collection->sort(); diff --git a/Tests/Strategy/StrategyTest.php b/Tests/Strategy/StrategyTest.php new file mode 100644 index 0000000..7e587b1 --- /dev/null +++ b/Tests/Strategy/StrategyTest.php @@ -0,0 +1,70 @@ + 2), array('id' => 1), array('id' => 3)), + array('id' => 1) + ), + array( + array(array('id' => 3), array('id' => 2), array('id' => 1)), + array('id' => 1) + ), + ); + } + + public function getDateCollection() + { + return array( + array( + array(array('date' => '2014-03-03'), array('date' => '2015-03-02'), array('date' => '2013-03-01')), + array('date' => '2013-03-01') + ), + array( + array(array('date' => '2014-02-03'), array('date' => '2013-02-01'), array('date' => '2015-02-02')), + array('date' => '2013-02-01') + ), + ); + } + + /** + * @dataProvider getIdCollection + */ + public function testIdComparator($collection, $expected) + { + $obj = new ObjectCollection($collection); + $obj->setComparator(new IdComparator()); + $elements = $obj->sort(); + + $firstElement = array_shift($elements); + $this->assertEquals($expected, $firstElement); + } + + /** + * @dataProvider getDateCollection + */ + public function testDateComparator($collection, $expected) + { + $obj = new ObjectCollection($collection); + $obj->setComparator(new DateComparator()); + $elements = $obj->sort(); + + $firstElement = array_shift($elements); + $this->assertEquals($expected, $firstElement); + } +} From 30c4e773d3c8d41dac4ccc752aace13e244521f6 Mon Sep 17 00:00:00 2001 From: bosoniq Date: Fri, 21 Mar 2014 18:39:20 +0100 Subject: [PATCH 06/12] Fixed misspelling of through modified: README.md --- Composite/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Composite/README.md b/Composite/README.md index 6d4cea3..ae22908 100755 --- a/Composite/README.md +++ b/Composite/README.md @@ -2,7 +2,7 @@ # Purpose -To treat a group of objects the same way as a single instance of the object. +To treat a group of objects the same way as a single instance of the object. # Examples From d779b1e86aae829101e3b2ed77672c7ff95b5df6 Mon Sep 17 00:00:00 2001 From: Jan Brecka Date: Mon, 24 Mar 2014 16:51:49 +0100 Subject: [PATCH 07/12] add object pool pattern --- Pool/Pool.php | 31 +++++++++++++++++++++++ Pool/Processor.php | 54 +++++++++++++++++++++++++++++++++++++++++ Pool/README.md | 9 +++++++ Pool/Worker.php | 22 +++++++++++++++++ Pool/index.php | 17 +++++++++++++ README.md | 1 + Tests/Pool/PoolTest.php | 32 ++++++++++++++++++++++++ 7 files changed, 166 insertions(+) create mode 100644 Pool/Pool.php create mode 100644 Pool/Processor.php create mode 100644 Pool/README.md create mode 100644 Pool/Worker.php create mode 100644 Pool/index.php create mode 100644 Tests/Pool/PoolTest.php diff --git a/Pool/Pool.php b/Pool/Pool.php new file mode 100644 index 0000000..a38da74 --- /dev/null +++ b/Pool/Pool.php @@ -0,0 +1,31 @@ +class = $class; + } + + public function get() + { + if (count($this->instances) > 0) { + return array_pop($this->instances); + } + + return new $this->class(); + } + + public function dispose($instance) + { + $this->instances[] = $instance; + } + +} + diff --git a/Pool/Processor.php b/Pool/Processor.php new file mode 100644 index 0000000..a3cac2d --- /dev/null +++ b/Pool/Processor.php @@ -0,0 +1,54 @@ +pool = $pool; + } + + public function process($image) + { + if ($this->processing++ < $this->maxProcesses) { + $this->createWorker($image); + } else { + $this->pushToWaitingQueue($worker); + } + } + + private function createWorker($image) + { + $worker = $this->pool->get(); + $worker->run($image, array($this, 'processDone')); + } + + public function processDone($worker) + { + $this->processing--; + $this->pool->dispose($worker); + + if (count($this->waitingQueue) > 0) { + $this->createWorker($this->popFromWaitingQueue()); + } + } + + private function pushToWaitingQueue($image) + { + $this->waitingQueue[] = $image; + } + + private function popFromWaitingQueue() + { + return array_pop($this->waitingQueue); + } + +} + diff --git a/Pool/README.md b/Pool/README.md new file mode 100644 index 0000000..b52fd87 --- /dev/null +++ b/Pool/README.md @@ -0,0 +1,9 @@ +Pool +==== + +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. + +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. + +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. + diff --git a/Pool/Worker.php b/Pool/Worker.php new file mode 100644 index 0000000..8ef0f79 --- /dev/null +++ b/Pool/Worker.php @@ -0,0 +1,22 @@ +process('image1.jpg'); +$processor->process('image2.jpg'); +$processor->process('image3.jpg'); +$processor->process('image4.jpg'); +$processor->process('image5.jpg'); +$processor->process('image6.jpg'); +$processor->process('image7.jpg'); +$processor->process('image8.jpg'); + diff --git a/README.md b/README.md index b34051e..35e565e 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ The patterns can be structured in roughly three different categories. Please cli * [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern) * [StaticFactory](StaticFactory) * [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern) +* [Pool](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern) * [Singleton](Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:) * [Multiton](Multiton) (is considered an anti-pattern! :no_entry:) diff --git a/Tests/Pool/PoolTest.php b/Tests/Pool/PoolTest.php new file mode 100644 index 0000000..0b86c5b --- /dev/null +++ b/Tests/Pool/PoolTest.php @@ -0,0 +1,32 @@ +get(); + + $this->assertEquals(1, $worker->id); + + $worker->id = 5; + $pool->dispose($worker); + + $this->assertEquals(5, $pool->get()->id); + $this->assertEquals(1, $pool->get()->id); + } + +} + From abba609136e6817c90c946750a2484066ae3cc4d Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Thu, 27 Mar 2014 12:40:48 +0100 Subject: [PATCH 08/12] Update README.md Added missing punctuation and formatted variable and class names. --- DependencyInjection/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/README.md b/DependencyInjection/README.md index ceca3d1..5c8b453 100644 --- a/DependencyInjection/README.md +++ b/DependencyInjection/README.md @@ -6,11 +6,11 @@ To implement a loosely coupled architecture in order to get better testable, mai ## Usage -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`. +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`. -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). +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). ## Examples -* 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 +* 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 * 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) From 6e937055cf5f66ea9337f96e85b036626c699b79 Mon Sep 17 00:00:00 2001 From: Jan Brecka Date: Tue, 8 Apr 2014 11:29:03 +0200 Subject: [PATCH 09/12] unifies indentation --- Pool/Pool.php | 25 ++++++++++++------------- Pool/Processor.php | 1 - Pool/README.md | 1 - Pool/Worker.php | 7 +++---- Pool/index.php | 1 - 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Pool/Pool.php b/Pool/Pool.php index a38da74..32de053 100644 --- a/Pool/Pool.php +++ b/Pool/Pool.php @@ -5,7 +5,7 @@ namespace DesignPatterns\Pool; class Pool { - private $instances = array(); + private $instances = array(); private $class; public function __construct($class) @@ -13,19 +13,18 @@ class Pool $this->class = $class; } - public function get() - { - if (count($this->instances) > 0) { - return array_pop($this->instances); - } + public function get() + { + if (count($this->instances) > 0) { + return array_pop($this->instances); + } - return new $this->class(); - } + return new $this->class(); + } - public function dispose($instance) - { - $this->instances[] = $instance; - } + public function dispose($instance) + { + $this->instances[] = $instance; + } } - diff --git a/Pool/Processor.php b/Pool/Processor.php index a3cac2d..eabd51d 100644 --- a/Pool/Processor.php +++ b/Pool/Processor.php @@ -51,4 +51,3 @@ class Processor } } - diff --git a/Pool/README.md b/Pool/README.md index b52fd87..a39c3b2 100644 --- a/Pool/README.md +++ b/Pool/README.md @@ -6,4 +6,3 @@ The **object pool pattern** is a software creational design pattern that uses a 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. 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. - diff --git a/Pool/Worker.php b/Pool/Worker.php index 8ef0f79..2eb4126 100644 --- a/Pool/Worker.php +++ b/Pool/Worker.php @@ -11,12 +11,11 @@ class Worker // for example creates "thread" } - public function run($image, array $callback) - { + public function run($image, array $callback) + { // do something with $image... // and when it's done, execute callback call_user_func($callback, $this); - } + } } - diff --git a/Pool/index.php b/Pool/index.php index a41c492..3059efe 100644 --- a/Pool/index.php +++ b/Pool/index.php @@ -14,4 +14,3 @@ $processor->process('image5.jpg'); $processor->process('image6.jpg'); $processor->process('image7.jpg'); $processor->process('image8.jpg'); - From 65e48b2d306d282451aba96d300731c48e286363 Mon Sep 17 00:00:00 2001 From: Jan Brecka Date: Tue, 8 Apr 2014 11:29:40 +0200 Subject: [PATCH 10/12] typo --- Pool/Worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pool/Worker.php b/Pool/Worker.php index 2eb4126..4d34a0d 100644 --- a/Pool/Worker.php +++ b/Pool/Worker.php @@ -7,7 +7,7 @@ class Worker public function __construct() { - // let say that constuctor does really expensive work... + // let's say that constuctor does really expensive work... // for example creates "thread" } From 7677d61e4e39153cbd474b86ab71aa97e061872c Mon Sep 17 00:00:00 2001 From: will-b Date: Wed, 9 Apr 2014 23:24:51 +0100 Subject: [PATCH 11/12] Typo in TemplateMethod/Journey.php comment sbclasses -> Subclasses --- TemplateMethod/Journey.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TemplateMethod/Journey.php b/TemplateMethod/Journey.php index f5e666d..54985d9 100644 --- a/TemplateMethod/Journey.php +++ b/TemplateMethod/Journey.php @@ -45,7 +45,7 @@ abstract class Journey } /** - * sbclasses will get access to this method but cannot override it and + * Subclasses will get access to this method but cannot override it and * compromise this algorithm (warning : cause of cyclic dependencies) */ final protected function takePlane() From 01954f1efa7444337a4aae5e4c851967fdb812d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0rfan=20Evrens?= Date: Fri, 11 Apr 2014 01:28:25 +0300 Subject: [PATCH 12/12] Prototype to Pool --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c5fa05d..56fd4df 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The patterns can be structured in roughly three different categories. Please cli * [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern) * [StaticFactory](StaticFactory) * [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern) -* [Pool](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern) +* [Pool](Pool) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern) * [Singleton](Singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:) * [Multiton](Multiton) (is considered an anti-pattern! :no_entry:)