The Iterator pattern is a technique that allows an aggregate object to provide the outside world with a way to access its collection of subobjects.
In Java we use java.util.Iterator interface to be implemented.
eg. StringTokenizer ResultSet
In Ruby We can write Iterator:
class ArrayIterator
def initialize(array)
@array = array
@index = 0
end
def has_next?
@index < @array.length
end
def item
@array[@index]
end
def next_item
value = @array[@index]
@index += 1
value
end
end
array = ['red', 'green', 'blue']
i = ArrayIterator.new(array)
while i.has_next?
puts("item: #{i.next_item}")
end
The Internal Iterator is:
def for_each_element(array)
i = 0
while i < array.length
yield(array[i])
i += 1
end
end
There are some differences between the two implementations:
external version will be easier to control the process:
"With an external iterator, you won’t call next until you are good and ready for the next element. With an internal iterator, by contrast, the aggregate relentlessly pushes the code block to accept item
after item."
"A second advantage of external iterators is that, because they are external, you can share them—you can pass them around to other methods and objects."
分享到:
相关推荐
Design Patterns in Ruby, Addison-wesley (2008). The Addison-Wesley Professional Ruby Series provides readers with practical, people-oriented, and in-depth information about applying the Ruby platform ...
Addison.Wesley.Design.Patterns.in.Ruby.Dec.2007 高清PDF英文版
Design Patterns in Ruby Dec 2007.rar
Design Patterns in Modern C++.pdf
design pattern in C# language
Introduction to Design Patterns in C With Qt4 Alan Ezust
Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design English | PDF| 2018 | 312 Pages | ISBN : 1484236025Design Patterns in Modern C++: Reusable Approaches for Object...
Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....
Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...
An introduction to Design Patterns in C++ with Qt 2nd Edition 中文版 C++ qt 设计模式 第二版, 中英文版 合集 Master C++ “The Qt Way” with Modern Design Patterns and Efficient Reuse This fully ...
Implement design patterns in .NET using the latest versions of the C# and F# languages. This book provides a comprehensive overview of the field of design patterns as they are used in today’s ...
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Design Patterns by Tutorials Learning design patterns in Swift 4.2, 2nd Edition.pdf
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
Reusable Approaches for Object-Oriented... Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply functional design patterns such as Monad and more
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
and applications that run on various software and hardware platforms with little or no change in the underlying codebase, while still being a native application with native capabilities and speed....