Iterator pattern
This commit is contained in:
parent
2ff56c4952
commit
08c61fdfc8
@ -111,6 +111,9 @@ Without violating encapsulation, capture and externalize an object's internal st
|
||||
### Interpretor
|
||||
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
|
||||
|
||||
### Iterator (Cursor)
|
||||
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
|
||||
|
||||
### Command
|
||||
Encapsulate a request as an object, thereby letting you parametrize clients with different requests, and support undoable operations.
|
||||
|
||||
|
82
behavioral/iterator/iterator.go
Normal file
82
behavioral/iterator/iterator.go
Normal file
@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//Iterator object should implement this interface
|
||||
type Iterator interface {
|
||||
First()
|
||||
Next() bool
|
||||
Done() bool
|
||||
CurrentItem() interface{}
|
||||
}
|
||||
|
||||
//A list object should implement Iterable interface in order to be a iterable list
|
||||
type Iterable interface {
|
||||
CreateIterator() Iterator
|
||||
}
|
||||
|
||||
//Iterator object
|
||||
type ListIterator struct {
|
||||
data []string
|
||||
index int
|
||||
}
|
||||
|
||||
func (i *ListIterator) Done() bool {
|
||||
return i.index >= len(i.data)
|
||||
}
|
||||
|
||||
func (i *ListIterator) CurrentItem() interface{} {
|
||||
return i.data[i.index]
|
||||
}
|
||||
|
||||
func (i *ListIterator) First() {
|
||||
i.index = 0
|
||||
}
|
||||
|
||||
func (i *ListIterator) Next() bool {
|
||||
i.index++
|
||||
if i.Done() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//Actual a basic list object.I don't use abstraction here.
|
||||
type List struct {
|
||||
data []string
|
||||
}
|
||||
|
||||
func (l *List) Append(str string) {
|
||||
l.data = append(l.data, str)
|
||||
}
|
||||
|
||||
func (l *List) Count() int {
|
||||
return len(l.data)
|
||||
}
|
||||
|
||||
func (l *List) CreateIterator() Iterator {
|
||||
listIterator := &ListIterator{data: l.data, index: -1}
|
||||
return listIterator
|
||||
}
|
||||
|
||||
//Basic testing....
|
||||
|
||||
func main() {
|
||||
lst := &List{data: make([]string, 0)}
|
||||
lst.Append("A")
|
||||
lst.Append("B")
|
||||
lst.Append("C")
|
||||
lst.Append("D")
|
||||
lst.Append("E")
|
||||
|
||||
//time to iterate...
|
||||
lstIterator := lst.CreateIterator()
|
||||
for lstIterator.Next() {
|
||||
fmt.Println("-", lstIterator.CurrentItem())
|
||||
}
|
||||
|
||||
lstIterator.First()
|
||||
fmt.Println("First:", lstIterator.CurrentItem())
|
||||
}
|
Reference in New Issue
Block a user