Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions AgregatorImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { IAgregator } from "./IAgregator";
import { IIterator } from "./IIterator";
import { IteratorImpl } from "./IteratorImpl";

//Реализуем наш "агрегатор"
export class AgregatorImpl implements IAgregator {
private items: any[] = [];

public createIterator(): IIterator<any> { // Вызываем итератор
return new IteratorImpl(this);
}
public addItem(item: any): void { // Добавляем элементы в коллекцию
this.items.push(item);
}
public getItem(): any[] { // Получаем элементы коллекции
return this.items;
}
}
7 changes: 7 additions & 0 deletions IAgregator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IIterator } from "./IIterator";

export interface IAgregator { // Собираем в интерфейс методы для создания коллекции
createIterator(): IIterator<any> // Создать итератор
addItem(item): void; // Добавить элемент
getItem(item): void // Получить элемент
}
4 changes: 4 additions & 0 deletions IIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IIterator<T> { // Определяет общий интерфейс для доступа и обхода элементов.
next(): T; // получить следующий элемент
hasNext(): boolean; // есть ли следующий элемент
}
17 changes: 17 additions & 0 deletions IteratorImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IIterator } from "./IIterator";
import { IAgregator } from "./IAgregator";

// Реализуем итератор
export class IteratorImpl implements IIterator<any> {
private collection: IAgregator;
private key: number = 0;
constructor(collection: IAgregator) { // создаем коллекцию
this.collection = collection;
}
public next(): any { // Возвращаем следующий элемент
return this.collection.getItem(this.key++);
}
public hasNext(): boolean { // Проверяем наличие следующего элемента
return this.collection.getItem(this.key++) ? true : false;
}
}
14 changes: 14 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AgregatorImpl } from "./AgregatorImpl";

const collection = new AgregatorImpl();
//Добавляем в коллекцию
collection.addItem(false); // bool
collection.addItem('Sun'); // string
collection.addItem([4, 3, 5]); // array
collection.addItem(10); // number

const iterator = collection.createIterator(); // Создаем итератор

while (iterator.hasNext()) { // Пока есть следующий элемент печатаем его
console.log(iterator.next());
}