mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 03:46:03 +03:00
update iterator pattern
This commit is contained in:
parent
0a186384f9
commit
64daf5eb8b
@ -26,7 +26,7 @@ type ScenicArea struct {
|
||||
//PotsIterator 该对象的目的就是为了隐藏景区本身
|
||||
//PotsIterator 实现为一个游标迭代器
|
||||
type PotsIterator struct {
|
||||
cursor int
|
||||
cursor, count int
|
||||
potsSlice []IPot
|
||||
}
|
||||
|
||||
@ -34,6 +34,7 @@ type PotsIterator struct {
|
||||
func (s *ScenicArea) Iterator() Iterator {
|
||||
return &PotsIterator{
|
||||
cursor: 0,
|
||||
count: s.count,
|
||||
potsSlice: s.pots,
|
||||
}
|
||||
}
|
||||
@ -63,16 +64,15 @@ func (s *PotsIterator) FirstPot() IPot {
|
||||
|
||||
//IsLastPot 判断游标的位置
|
||||
func (s *PotsIterator) IsLastPot() bool {
|
||||
return s.cursor == -1
|
||||
return s.cursor == s.count
|
||||
}
|
||||
|
||||
//Next 去路线上的下一个景点
|
||||
func (s *PotsIterator) Next() IPot {
|
||||
if s.cursor+1 == len(s.potsSlice) {
|
||||
s.cursor = -1 //设置到最后
|
||||
s.cursor++
|
||||
if s.IsLastPot() {
|
||||
return nil
|
||||
}
|
||||
s.cursor++
|
||||
return s.potsSlice[s.cursor]
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user