From 1903b4405a33059f08c6214e0fe6db4541e77d6a Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Thu, 20 May 2021 21:34:32 +0300 Subject: [PATCH 1/3] iterator pattern --- iterator/ts/index.ts | 104 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 iterator/ts/index.ts diff --git a/iterator/ts/index.ts b/iterator/ts/index.ts new file mode 100644 index 0000000..3214c5a --- /dev/null +++ b/iterator/ts/index.ts @@ -0,0 +1,104 @@ +/** + * Iterator pattern + * + * Purpose: Allows you to sequentially bypass the elements of the composite + * objects without revealing their internal representation. + */ + +interface Iterator { + // Returns the current item. + current(): T; + + // Returns the current item and moves to the next item. + next(): T; + + // Returns the key of the current item. + key(): number; + + // Checks the correctness of the current position. + valid(): boolean; + + // Rewind the Iterator to the first item. + rewind(): void; +} + +interface Aggregator { + // Get an external iterator. + getIterator(): Iterator; +} + +/** + * Concrete Iterators implement different traversal algorithms. These classes + * permanently store the current bypass position. + */ + +class CollectionIterator implements Iterator { + private collection: Collection; + + + //Stores the current bypass position. + private position: number = 0; + + constructor(collection: Collection) { + this.collection = collection; + } + + public rewind() { + this.position = 0; + } + + public current(): any { + return this.collection.getItems()[this.position]; + } + + public key(): number { + return this.position; + } + + public next(): any { + const item = this.collection.getItems()[this.position]; + this.position += 1; + return item; + } + + public valid(): boolean { + + return this.position < this.collection.getCount(); + } +} + +/** + * Concrete Collections provide one or more methods to get + * new iterator instances compatible with the collection class. + */ +class Collection implements Aggregator { + private items: any[] = []; + + public getItems(): any[] { + return this.items; + } + + public getCount(): number { + return this.items.length; + } + + public addItem(item: any): void { + this.items.push(item); + } + + public getIterator(): Iterator { + return new CollectionIterator(this); + } + +} + +const collection = new Collection(); +collection.addItem(3); +collection.addItem('String'); +collection.addItem([4, 6]); + +const iterator = collection.getIterator(); + +while (iterator.valid()) { + console.log(iterator.next()); +} From 9b3a836929428dd3b21c2c17b5978dcf294db3d1 Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Thu, 27 May 2021 00:22:39 +0300 Subject: [PATCH 2/3] some fix --- iterator/ts/aggregator.ts | 6 ++ iterator/ts/collection.ts | 33 +++++++++++ iterator/ts/collectionIterator.ts | 46 ++++++++++++++++ iterator/ts/index.ts | 91 +------------------------------ iterator/ts/iterator.ts | 16 ++++++ 5 files changed, 104 insertions(+), 88 deletions(-) create mode 100644 iterator/ts/aggregator.ts create mode 100644 iterator/ts/collection.ts create mode 100644 iterator/ts/collectionIterator.ts create mode 100644 iterator/ts/iterator.ts diff --git a/iterator/ts/aggregator.ts b/iterator/ts/aggregator.ts new file mode 100644 index 0000000..c1d371b --- /dev/null +++ b/iterator/ts/aggregator.ts @@ -0,0 +1,6 @@ +import {Iterator} from "./iterator"; + +export interface Aggregator { + // Get an external iterator. + getIterator(): Iterator; +} \ No newline at end of file diff --git a/iterator/ts/collection.ts b/iterator/ts/collection.ts new file mode 100644 index 0000000..24fa883 --- /dev/null +++ b/iterator/ts/collection.ts @@ -0,0 +1,33 @@ +import {Aggregator} from "./aggregator"; +import {CollectionIterator} from "./collectionIterator"; +import {Iterator} from "./iterator"; + +/** + * Concrete Collections provide one or more methods to get + * new iterator instances compatible with the collection class. + */ + +export class Collection implements Aggregator { + + private items: any[] = []; + + //Get all items + public getItems(): any[] { + return this.items; + } + + //Get length + public getCount(): number { + return this.items.length; + } + + //Add item to collection + public addItem(item: any): void { + this.items.push(item); + } + + public getIterator(): Iterator { + return new CollectionIterator(this); + } + +} \ No newline at end of file diff --git a/iterator/ts/collectionIterator.ts b/iterator/ts/collectionIterator.ts new file mode 100644 index 0000000..e34fafd --- /dev/null +++ b/iterator/ts/collectionIterator.ts @@ -0,0 +1,46 @@ +import {Collection} from "./collection"; +import {Iterator} from "./iterator"; +/** + * Concrete Iterators implement different traversal algorithms. These classes + * permanently store the current bypass position. + */ + +export class CollectionIterator implements Iterator { + private collection: Collection; + + + //Stores the current bypass position + private position: number = 0; + + constructor(collection: Collection) { + this.collection = collection; + } + + //Updates the position + public rewind() { + this.position = 0; + } + + //Return current position + public current(): any { + return this.collection.getItems()[this.position]; + } + + //Return key of position + public key(): number { + return this.position; + } + + //Return next item + public next(): any { + const item = this.collection.getItems()[this.position]; + this.position += 1; + return item; + } + + //Check for next item + public hasNext(): boolean { + + return this.position < this.collection.getCount(); + } +} \ No newline at end of file diff --git a/iterator/ts/index.ts b/iterator/ts/index.ts index 3214c5a..004ed3d 100644 --- a/iterator/ts/index.ts +++ b/iterator/ts/index.ts @@ -1,3 +1,5 @@ +import {Collection} from "./collection"; + /** * Iterator pattern * @@ -5,93 +7,6 @@ * objects without revealing their internal representation. */ -interface Iterator { - // Returns the current item. - current(): T; - - // Returns the current item and moves to the next item. - next(): T; - - // Returns the key of the current item. - key(): number; - - // Checks the correctness of the current position. - valid(): boolean; - - // Rewind the Iterator to the first item. - rewind(): void; -} - -interface Aggregator { - // Get an external iterator. - getIterator(): Iterator; -} - -/** - * Concrete Iterators implement different traversal algorithms. These classes - * permanently store the current bypass position. - */ - -class CollectionIterator implements Iterator { - private collection: Collection; - - - //Stores the current bypass position. - private position: number = 0; - - constructor(collection: Collection) { - this.collection = collection; - } - - public rewind() { - this.position = 0; - } - - public current(): any { - return this.collection.getItems()[this.position]; - } - - public key(): number { - return this.position; - } - - public next(): any { - const item = this.collection.getItems()[this.position]; - this.position += 1; - return item; - } - - public valid(): boolean { - - return this.position < this.collection.getCount(); - } -} - -/** - * Concrete Collections provide one or more methods to get - * new iterator instances compatible with the collection class. - */ -class Collection implements Aggregator { - private items: any[] = []; - - public getItems(): any[] { - return this.items; - } - - public getCount(): number { - return this.items.length; - } - - public addItem(item: any): void { - this.items.push(item); - } - - public getIterator(): Iterator { - return new CollectionIterator(this); - } - -} - const collection = new Collection(); collection.addItem(3); collection.addItem('String'); @@ -99,6 +14,6 @@ collection.addItem([4, 6]); const iterator = collection.getIterator(); -while (iterator.valid()) { +while (iterator.hasNext()) { console.log(iterator.next()); } diff --git a/iterator/ts/iterator.ts b/iterator/ts/iterator.ts new file mode 100644 index 0000000..87fb9d2 --- /dev/null +++ b/iterator/ts/iterator.ts @@ -0,0 +1,16 @@ +export interface Iterator { + // Returns the current item + current(): T; + + // Returns the current item and moves to the next item + next(): T; + + // Returns the key of the current item + key(): number; + + // Checks the correctness of the current position + hasNext(): boolean; + + // Rewind the Iterator to the first item + rewind(): void; +} \ No newline at end of file From b38105d34904b2273f82ea4a3daea414eb0869f4 Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Thu, 27 May 2021 00:24:49 +0300 Subject: [PATCH 3/3] some fix --- iterator/ts/collection.ts | 2 +- iterator/ts/collectionIterator.ts | 3 +-- iterator/ts/index.ts | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/iterator/ts/collection.ts b/iterator/ts/collection.ts index 24fa883..5d1306f 100644 --- a/iterator/ts/collection.ts +++ b/iterator/ts/collection.ts @@ -6,7 +6,6 @@ import {Iterator} from "./iterator"; * Concrete Collections provide one or more methods to get * new iterator instances compatible with the collection class. */ - export class Collection implements Aggregator { private items: any[] = []; @@ -26,6 +25,7 @@ export class Collection implements Aggregator { this.items.push(item); } + //Get iterator public getIterator(): Iterator { return new CollectionIterator(this); } diff --git a/iterator/ts/collectionIterator.ts b/iterator/ts/collectionIterator.ts index e34fafd..3821f95 100644 --- a/iterator/ts/collectionIterator.ts +++ b/iterator/ts/collectionIterator.ts @@ -1,10 +1,10 @@ import {Collection} from "./collection"; import {Iterator} from "./iterator"; + /** * Concrete Iterators implement different traversal algorithms. These classes * permanently store the current bypass position. */ - export class CollectionIterator implements Iterator { private collection: Collection; @@ -40,7 +40,6 @@ export class CollectionIterator implements Iterator { //Check for next item public hasNext(): boolean { - return this.position < this.collection.getCount(); } } \ No newline at end of file diff --git a/iterator/ts/index.ts b/iterator/ts/index.ts index 004ed3d..2e38356 100644 --- a/iterator/ts/index.ts +++ b/iterator/ts/index.ts @@ -6,7 +6,6 @@ import {Collection} from "./collection"; * Purpose: Allows you to sequentially bypass the elements of the composite * objects without revealing their internal representation. */ - const collection = new Collection(); collection.addItem(3); collection.addItem('String');