iterator

package
v0.0.0-...-6db54d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2021 License: MIT Imports: 0 Imported by: 0

README

Итератор (Iterator)

Паттерн Iterator относится к поведенческим паттернам уровня объекта.

Паттерн Iterator предоставляет механизм обхода коллекций объектов не раскрывая их внутреннего представления.

Зачастую этот паттерн используется вместо массива объектов, чтобы не только предоставить доступ к элементам, но и наделить некоторой логикой.

Iterator представляет собой общий интерфейс, позволяющий реализовать произвольную логику итераций.

Требуется для реализации:

  1. Интерфейс Iterator описывающий набор методов для доступа к коллекции;
  2. Класс ConcreteIterator, реализующий интерфейс Iterator. Следит за позицией текущего элемента при переборе коллекции (Aggregate).;
  3. Интерфейс Aggregate описывающий набор методов коллекции объектов;
  4. Класс ConcreteAggregate, реализующий интерфейс Aggregate и хранящий в себе элементы коллекции.

[!] В описании паттерна применяются общие понятия, такие как Класс, Объект, Абстрактный класс. Применимо к языку Go, это Пользовательский Тип, Значение этого Типа и Интерфейс. Также в языке Go за место общепринятого наследования используется агрегирование и встраивание.

-- THE END --

Documentation

Overview

Package iterator is an example of the Iterator Pattern.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregate

type Aggregate interface {
	Iterator() Iterator
}

Aggregate provides a collection interface.

type Book

type Book struct {
	Name string
}

Book implements a item of the collection.

type BookIterator

type BookIterator struct {
	// contains filtered or unexported fields
}

BookIterator implements the Iterator interface.

func (*BookIterator) End

func (i *BookIterator) End()

End goes to the last item.

func (*BookIterator) Has

func (i *BookIterator) Has() bool

Has implementation.

func (*BookIterator) Index

func (i *BookIterator) Index() int

Index returns current index

func (*BookIterator) Next

func (i *BookIterator) Next()

Next goes to the next item.

func (*BookIterator) Prev

func (i *BookIterator) Prev()

Prev goes to the previous item.

func (*BookIterator) Reset

func (i *BookIterator) Reset()

Reset resets iterator.

func (*BookIterator) Value

func (i *BookIterator) Value() interface{}

Value returns current value

type BookShelf

type BookShelf struct {
	Books []*Book
}

BookShelf implements the Aggregate interface.

func (*BookShelf) Add

func (b *BookShelf) Add(book *Book)

Add adds an item to the collection.

func (*BookShelf) Iterator

func (b *BookShelf) Iterator() Iterator

Iterator creates and returns the iterator over the collection.

type Iterator

type Iterator interface {
	Index() int
	Value() interface{}
	Has() bool
	Next()
	Prev()
	Reset()
	End()
}

Iterator provides a iterator interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL