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