Skip to content

Commit e36e678

Browse files
authored
Merge pull request #21 from bvwells/iterator
Implement iterator design pattern #12
2 parents 9283fa2 + c9370aa commit e36e678

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Name | Description | Status
6464
[`Chain of Responsibility`](./behavioral/chain_of_responsibility.go) | Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. | Implemented
6565
[`Command`](./behavioral/command.go) | Encapsulate a request as an object, thereby allowing for the parameterization of clients with different requests, and the queuing or logging of requests. It also allows for the support of undoable operations. | Implemented
6666
[`Interpreter`](./behavioral/interpreter.go) | Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. |
67-
[`Iterator`](./behavioral/iterator.go) | Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. |
67+
[`Iterator`](./behavioral/iterator.go) | Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. | Implemented
6868
[`Mediator`](./behavioral/mediator.go) | Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to vary independently. |
6969
[`Memento`](./behavioral/memento.go) | Without violating encapsulation, capture and externalize an object's internal state allowing the object to be restored to this state later. |
7070
[`Observer`](./behavioral/observer.go) | Define a one-to-many dependency between objects where a state change in one object results in all its dependents being notified and updated automatically. | Implemented

behavioral/iterator.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11
package behavioral
2+
3+
// Iterator is an interface for an iterator.
4+
type Iterator interface {
5+
6+
// Index returns the index of the current iterator.
7+
Index() int
8+
9+
// Value returns the current value of the iterator.
10+
Value() interface{}
11+
12+
// HasNext returns whether another next element exists.
13+
HasNext() bool
14+
15+
// Next increments the iterator to point to the next element.
16+
Next()
17+
}
18+
19+
// ArrayIterator is an iterator which iterates over an array.
20+
type ArrayIterator struct {
21+
array []interface{}
22+
index int
23+
}
24+
25+
// Index returns the index of the current iterator.
26+
func (i *ArrayIterator) Index() int {
27+
return i.index
28+
}
29+
30+
// Value returns the current value of the iterator.
31+
func (i *ArrayIterator) Value() interface{} {
32+
return i.array[i.index]
33+
}
34+
35+
// HasNext returns whether another next element exists.
36+
func (i *ArrayIterator) HasNext() bool {
37+
if i.index+1 == len(i.array) {
38+
return false
39+
}
40+
return true
41+
}
42+
43+
// Next increments the iterator to point to the next element.
44+
func (i *ArrayIterator) Next() {
45+
if i.HasNext() {
46+
i.index++
47+
}
48+
}

behavioral/iterator_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package behavioral
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIterator(t *testing.T) {
8+
9+
array := []interface{}{10.0, 20.0, 30.0, 40.0, 50.0}
10+
11+
iterator := ArrayIterator{array, 0}
12+
13+
for it := iterator; iterator.HasNext(); iterator.Next() {
14+
index, value := it.Index(), it.Value().(float64)
15+
if value != array[index] {
16+
t.Errorf("Expected array value to equal %v, but recieved %v", array[index], value)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)