forked from senghoo/golang-design-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 送代器模式 | ||
|
||
送代器模式用于使用相同方式送代不同类型集合或者隐藏集合类型的具体实现。 | ||
|
||
可以使用送代器模式使遍历同时应用送代策略,如请求新对象、过滤、处理对象等。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package iterator | ||
|
||
import "fmt" | ||
|
||
type Aggregate interface { | ||
Iterator() Iterator | ||
} | ||
|
||
type Iterator interface { | ||
First() | ||
IsDone() bool | ||
Next() interface{} | ||
} | ||
|
||
type Numbers struct { | ||
start, end int | ||
} | ||
|
||
func NewNumbers(start, end int) *Numbers { | ||
return &Numbers{ | ||
start: start, | ||
end: end, | ||
} | ||
} | ||
|
||
func (n *Numbers) Iterator() Iterator { | ||
return &NumbersIterator{ | ||
numbers: n, | ||
next: n.start, | ||
} | ||
} | ||
|
||
type NumbersIterator struct { | ||
numbers *Numbers | ||
next int | ||
} | ||
|
||
func (i *NumbersIterator) First() { | ||
i.next = i.numbers.start | ||
} | ||
|
||
func (i *NumbersIterator) IsDone() bool { | ||
return i.next > i.numbers.end | ||
} | ||
|
||
func (i *NumbersIterator) Next() interface{} { | ||
if !i.IsDone() { | ||
next := i.next | ||
i.next++ | ||
return next | ||
} | ||
return nil | ||
} | ||
|
||
func IteratorPrint(i Iterator) { | ||
for i.First(); !i.IsDone(); { | ||
c := i.Next() | ||
fmt.Printf("%#v\n", c) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package iterator | ||
|
||
func ExampleIterator() { | ||
var aggregate Aggregate | ||
aggregate = NewNumbers(1, 10) | ||
|
||
IteratorPrint(aggregate.Iterator()) | ||
// Output: | ||
// 1 | ||
// 2 | ||
// 3 | ||
// 4 | ||
// 5 | ||
// 6 | ||
// 7 | ||
// 8 | ||
// 9 | ||
// 10 | ||
} |