diff --git a/README.md b/README.md index 0cb0f47..4082ebb 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/behavioral/iterator/iterator.go b/behavioral/iterator/iterator.go new file mode 100644 index 0000000..3083e70 --- /dev/null +++ b/behavioral/iterator/iterator.go @@ -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()) +}