Iterator (tekrarlayıcı) tasarım şablonu davranışsal (Behavioral Patterns) tasarım şablonları grubunda yer alır. Listede yer alan nesnelere sırayla erişebilir ve bu nesneler üzerinde işlem yapabiliriz.
php5 bu tasarım şablonunu kendi yapısındada The Standard PHP Library (SPL) altında bulundurmaktadır. SPL yi ileriki zamanlarda anlatacağım biz bu yazımızda iteratorun çalışma mantığını inceleyeceğiniz.
bu patternin aslında çok anlatılacak bir olayı yok. bi array’miz olduğunu düşünün bu patternin arraydeki elemanlar arasında gezinmemize yarar.
getNext() ile bir sonraki elemana geçeriz.
getCurrent() üzerinde bulundğumuz elemanı verir.
hasNext() ile ileride eleman olup olmadığına bakarız
add() ile eleman ekleriz
remove() ile eleman sileriz
iterator sınıfımız
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class ListeIterator { protected $list; protected $current = 0; public function __construct(Liste $list) { $this->list = $list; } public function getCurrent() { if (($this->current > 0) && ($this->list->getCount() >= $this->current)) return $this->list->get($this->current); } public function getNext() { if ($this->hasNext()) return $this->list->get(++$this->current); return NULL; } public function hasNext() { if ($this->list->getCount() > $this->current) return TRUE; return FALSE; } } |
liste sınıfımız
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class ListeIterator { protected $list; protected $current = 0; public function __construct(Liste $list) { $this->list = $list; } public function getCurrent() { if (($this->current > 0) && ($this->list->getCount() >= $this->current)) return $this->list->get($this->current); } public function getNext() { if ($this->hasNext()) return $this->list->get(++$this->current); return NULL; } public function hasNext() { if ($this->list->getCount() > $this->current) return TRUE; return FALSE; } } |
user sınıfımız
1 2 3 4 5 6 7 8 9 10 | class User { private $name; function __construct($name) { $this->name= $name; } function getName() {return $this->name;} } |
şimdi uygulamamızı çalıştıracak kodları yazalım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $user1 = new User('Emre Çevik'); $user2 = new User('Erdem Çevik'); $user3 = new User('Atakan Maden'); $user4 = new User('Hasan Kurt'); $liste = new Liste(); $liste->add($user1); $liste->add($user2); $liste->add($user3); $liste->add($user4); $listeIterator = new ListeIterator($liste); while ($listeIterator->hasNext()) { $liste = $listeIterator->getNext(); echo $liste->getName().'<br>'; } $liste = $listeIterator->getCurrent(); echo $liste->getName().'<br>'; |
kodumuzun ekran çıktısı
Emre Çevik
Erdem Çevik
Atakan Maden
Hasan Kurt
Hasan Kurt
dosya : iterator design patter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | <?php class User { private $name; function __construct($name) { $this->name= $name; } function getName() {return $this->name;} } class Liste { private $array = array(); private $count = 0; public function __construct() {} public function getCount() { return $this->count; } private function setCount($number) { $this->count = $number; } public function get($number) { if ( (is_numeric($number)) && ($number <= $this->getCount())) return $this->array[$number]; return NULL; } public function add(User $name) { $this->setCount($this->getCount() + 1); $this->array[$this->getCount()] = $name; return $this->getCount(); } public function remove(User $name) { $counter = 0; while (++$counter <= $this->getCount()) if ($name->getName() == $this->array[$counter]->getName()) for ($x = $counter; $x < $this->getCount(); $x++) $this->array[$x] = $this->array[$x + 1]; $this->setCount($this->getCount() - 1); return $this->getCount(); } } class ListeIterator { protected $list; protected $current = 0; public function __construct(Liste $list) { $this->list = $list; } public function getCurrent() { if (($this->current > 0) && ($this->list->getCount() >= $this->current)) return $this->list->get($this->current); } public function getNext() { if ($this->hasNext()) return $this->list->get(++$this->current); return NULL; } public function hasNext() { if ($this->list->getCount() > $this->current) return TRUE; return FALSE; } } $user1 = new User('Emre Çevik'); $user2 = new User('Erdem Çevik'); $user3 = new User('Atakan Maden'); $user4 = new User('Hasan Kurt'); $liste = new Liste(); $liste->add($user1); $liste->add($user2); $liste->add($user3); $liste->add($user4); $listeIterator = new ListeIterator($liste); while ($listeIterator->hasNext()) { $liste = $listeIterator->getNext(); echo $liste->getName().'<br>'; } $liste = $listeIterator->getCurrent(); echo $liste->getName().'<br>'; ?> |
benzer konular:
- observer design pattern – php
- decorator design pattern – php
- command design pattern – php
- state design pattern – php
- template method design pattern – php
- chain of responsibility design pattern- php
- strategy design pattern – php
- bridge design pattern – php
- adapter design pattern – php
- memento design pattern – php














design patterns