Skip to content

Commit 9484868

Browse files
author
Dominik Liebler
committed
cs Iterator and Registry
1 parent 4b66417 commit 9484868

File tree

7 files changed

+118
-160
lines changed

7 files changed

+118
-160
lines changed

Iterator/CardGame.php

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

Iterator/File.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace DesignPatterns\Iterator;
4+
5+
/**
6+
* iterator pattern
7+
*
8+
* Purpose:
9+
* to make an object iterable
10+
*
11+
* Examples:
12+
* - to process a file line by line by just running over all lines (which have an object representation) for a file
13+
* (which of course is an object, too)
14+
*
15+
* Note:
16+
* Standard PHP Library (SPL) defines an interface Iterator which is best suited for this!
17+
* Often you would want to implement the Countable interface too, to allow count($object) on your iterable object
18+
*
19+
* THIS EXAMPLE ALSO APPLIES THE COMPOSITE PATTERN
20+
*
21+
*/
22+
class File
23+
{
24+
/**
25+
* @var RowSet
26+
*/
27+
protected $rowSet;
28+
29+
/**
30+
* @var string
31+
*/
32+
protected $pathName;
33+
34+
/**
35+
* @param string $pathName
36+
*/
37+
public function __construct($pathName)
38+
{
39+
$this->rowSet = new Rowset($this);
40+
}
41+
42+
/**
43+
* processes the rowSet
44+
*/
45+
public function process()
46+
{
47+
// this is the place to show how using an iterator, with foreach
48+
// See the CardGame.php file
49+
$this->rowSet->process();
50+
}
51+
}

Iterator/Row.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace DesignPatterns\Iterator;
4+
5+
/**
6+
* Class Row
7+
*/
8+
class Row
9+
{
10+
protected $data;
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function __construct($data)
16+
{
17+
$this->data = $data;
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function process()
24+
{
25+
// do some fancy things here ...
26+
}
27+
}
Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,11 @@
11
<?php
22

3-
namespace DesignPatterns;
3+
namespace DesignPatterns\Iterator;
44

55
/**
6-
* iterator pattern
7-
*
8-
* Purpose:
9-
* to make an object iterable
10-
*
11-
* Examples:
12-
* - to process a file line by line by just running over all lines (which have an object representation) for a file
13-
* (which of course is an object, too)
14-
*
15-
* Note:
16-
* Standard PHP Library (SPL) defines an interface Iterator which is best suited for this!
17-
* Often you would want to implement the Countable interface too, to allow count($object) on your iterable object
18-
*
19-
* THIS EXAMPLE ALSO APPLIES THE COMPOSITE PATTERN
20-
*
6+
* Class RowSet
217
*/
22-
class File
23-
{
24-
/**
25-
* @var RowSet
26-
*/
27-
protected $rowSet;
28-
29-
/**
30-
* @var string
31-
*/
32-
protected $pathName;
33-
34-
/**
35-
* @param string $pathName
36-
*/
37-
public function __construct($pathName)
38-
{
39-
$this->rowSet = new Rowset($this);
40-
}
41-
42-
public function process()
43-
{
44-
// this is the place to show how using an iterator, with foreach
45-
// See the CardGame.php file
46-
$this->rowSet->process();
47-
}
48-
}
49-
50-
class Rowset implements \Iterator
8+
class RowSet implements \Iterator
519
{
5210
/**
5311
* @var
@@ -59,6 +17,11 @@ class Rowset implements \Iterator
5917
*/
6018
protected $file;
6119

20+
/**
21+
* @var int
22+
*/
23+
protected $lineNumber;
24+
6225
/**
6326
* @param string $file
6427
*/
@@ -79,20 +42,26 @@ public function process()
7942
* THE key feature of the Iterator Pattern is to provide a *public contract*
8043
* to iterate on a collection without knowing how items are handled inside
8144
* the collection. It is not just an easy way to use "foreach"
82-
*
45+
*
8346
* One cannot see the point of iterator pattern if you iterate on $this.
84-
* This example is unclear and mixed with some Composite pattern ideas.
47+
* This example is unclear and mixed with some Composite pattern ideas.
8548
*/
8649
foreach ($this as $line => $row) {
8750
$row->process();
8851
}
8952
}
9053

54+
/**
55+
* {@inheritdoc}
56+
*/
9157
public function rewind()
9258
{
9359
// seek to first line from $this->file
9460
}
9561

62+
/**
63+
* {@inheritdoc}
64+
*/
9665
public function next()
9766
{
9867
// read the next line from $this->file
@@ -104,34 +73,28 @@ public function next()
10473
}
10574
}
10675

76+
/**
77+
* {@inheritdoc}
78+
*/
10779
public function current()
10880
{
10981
return $this->currentRow;
11082
}
11183

84+
/**
85+
* {@inheritdoc}
86+
*/
11287
public function valid()
11388
{
11489
return null !== $this->currentRow;
11590
}
11691

92+
/**
93+
* {@inheritdoc}
94+
*/
11795
public function key()
11896
{
11997
// you would want to increment this in next() or whatsoever
120-
return $this->_lineNumber;
121-
}
122-
}
123-
124-
class Row
125-
{
126-
protected $_data;
127-
128-
public function __construct($data)
129-
{
130-
$this->_data = $data;
131-
}
132-
133-
public function process()
134-
{
135-
// do some fancy things here ...
98+
return $this->lineNumber;
13699
}
137100
}

Multiton/Multiton.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Multiton
3737
*
3838
* @var array
3939
*/
40-
private static $_instances = array();
40+
private static $instances = array();
4141

4242
/**
4343
* should not be called from outside: private!
@@ -53,15 +53,16 @@ private function __construct()
5353
* uses lazy initialization
5454
*
5555
* @param string $instanceName
56+
*
5657
* @return Multiton
5758
*/
5859
public static function getInstance($instanceName)
5960
{
60-
if ( ! array_key_exists($instanceName, self::$_instances)) {
61-
self::$_instances[$instanceName] = new self();
61+
if (!array_key_exists($instanceName, self::$instances)) {
62+
self::$instances[$instanceName] = new self();
6263
}
6364

64-
return self::$_instances[$instanceName];
65+
return self::$instances[$instanceName];
6566
}
6667

6768
/**

Registry/Registry.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,3 @@ public static function get($key)
5252

5353
// typically there would be methods to check if a key has already been registered and so on ...
5454
}
55-
56-
// while bootstrapping the application
57-
Registry::set(Registry::LOGGER, new \StdClass());
58-
59-
// throughout the application
60-
Registry::get(Registry::LOGGER)->log('foo');

Registry/index.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use DesignPatterns\Registry;
4+
5+
// while bootstrapping the application
6+
Registry::set(Registry::LOGGER, new \StdClass());
7+
8+
// throughout the application
9+
Registry::get(Registry::LOGGER)->log('foo');

0 commit comments

Comments
 (0)