
Iterator w języku Ruby
Iterator to behawioralny wzorzec projektowy pozwalający sekwencyjnie przechodzić od elementu do elementu jakiegoś zbioru bez konieczności eksponowania jego formy.
Dzięki Iteratorowi klienci mogą przeglądać kolejne elementy różnych kolekcji w podobny sposób, za pośrednictwem jednego interfejsu.
Złożoność:
Popularność:
Przykłady użycia: Wzorzec Iterator jest bardzo rozpowszechniony w kodzie Ruby. Wiele frameworków i bibliotek pozwala za jego pomocą poruszać się po elementach ich kolekcji.
Identyfikacja: Iterator łatwo rozpoznać po obecności metod nawigacyjnych (takich jak następny
, poprzedni
i innych). Kod klienta stosujący iteratory może nie mieć bezpośredniego dostępu do badanej kolekcji.
Przykład koncepcyjny
Poniższy przykład ilustruje strukturę wzorca Iterator ze szczególnym naciskiem na następujące kwestie:
- Z jakich składa się klas?
- Jakie role pełnią te klasy?
- W jaki sposób elementy wzorca są ze sobą powiązane?
main.rb: Przykład koncepcyjny
class AlphabeticalOrderIterator
# In Ruby, the Enumerable mixin provides classes with several traversal and
# searching methods, and with the ability to sort. The class must provide a
# method each, which yields successive members of the collection.
include Enumerable
# This attribute indicates the traversal direction.
attr_accessor :reverse
private :reverse
# @return [Array]
attr_accessor :collection
private :collection
# @param [Array] collection
# @param [Boolean] reverse
def initialize(collection, reverse: false)
@collection = collection
@reverse = reverse
end
def each(&block)
return @collection.reverse.each(&block) if reverse
@collection.each(&block)
end
end
class WordsCollection
# @return [Array]
attr_accessor :collection
private :collection
def initialize(collection = [])
@collection = collection
end
# The `iterator` method returns the iterator object itself, by default we
# return the iterator in ascending order.
def iterator
AlphabeticalOrderIterator.new(@collection)
end
# @return [AlphabeticalOrderIterator]
def reverse_iterator
AlphabeticalOrderIterator.new(@collection, reverse: true)
end
# @param [String] item
def add_item(item)
@collection << item
end
end
# The client code may or may not know about the Concrete Iterator or Collection
# classes, depending on the level of indirection you want to keep in your
# program.
collection = WordsCollection.new
collection.add_item('First')
collection.add_item('Second')
collection.add_item('Third')
puts 'Straight traversal:'
collection.iterator.each { |item| puts item }
puts "\n"
puts 'Reverse traversal:'
collection.reverse_iterator.each { |item| puts item }
output.txt: Wynik działania
Straight traversal:
First
Second
Third
Reverse traversal:
Third
Second
First