Groovy 设计模式 -- 迭代器模式

 

Iterator Pattern

http://groovy-lang.org/design-patterns.html#_flyweight_pattern

 

迭代器模式,允许顺序访问 聚集对象中的中元素, 而不用关心起内部是如何实现的。

groovy有自己的语言内置的闭包操作, 例如 each

The Iterator Pattern allows sequential access to the elements of an aggregate object without exposing its underlying representation.

Groovy has the iterator pattern built right in to many of its closure operators, e.g. each and eachWithIndex as well as the for .. in loop.

 

 

例子

def printAll(container) { for (item in container) { println item } } def numbers = [ 1,2,3,4 ] def months = [ Mar:31, Apr:30, May:31 ] def colors = [ java.awt.Color.BLACK, java.awt.Color.WHITE ] printAll numbers printAll months printAll colors

 

 

colors.eachWithIndex { item, pos -> println "Position $pos contains '$item'" }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值