Skip to content

This is a Typescript EX that practices Iterator-design-pattern.

Notifications You must be signed in to change notification settings

LiatHanunu/Iterator-Design-Pattern-EX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Iterator Design-Pattern EX

This EX is a Typescript EX that practices Iterator-design-pattern.

There are a lot of data structures/collections available in every language and Each collection must provide an iterator that lets it iterate through its objects, However while doing so it should make sure that it does not expose its implementation.

which is where Iterator Pattern comes in handy.
Iterator is a behavioral design pattern that allows you to traverse collection elements without revealing their underlying representation (list, stack, tree, etc.).

In this Ex I implement the use of iterator design-pattern, using TypeScript.

Ex instructions

  • Create Contact.ts which is a container for phonebook contacts (id, name, address, phones)

  • Create IPhonebook.ts for managing phonebook of contacts.

  • size:number - gets the amount of contacts
  • add(contact:Contact):number - add contact and returns its new id
  • addPhone(id:number, phone:string):void - add new phone to existing contact
  • get(id:number):Contact|undefined - get contact by id
  • get(name:string):Contact[]|undefined - get contacts by name
  • remove(id:number):Contact|undefined - remove contact by id and returns it
  • Create Phonebook.ts implementing IPhonebook interface. But don't export Phonebook. Instead, export a function that returns an instance implementing IPhonebook: createPhonebook():IPhonebook

  • in main.ts:

  • import the function and get an instance of phonebook. (Notice, you'll be using the phonebook but only via the IPhonebook interface!)
  • Add manually 11 contacts, few with same name (e.g. {'Shahar', 'Ben-Gurion 14, Netanya', ['09-43433']} ) *Keep the data as realistic as possible
  • add 2 new contacts: 1 with existing name, 1 with new name check the size is updated

  • add phone to existing contact

  • get contacts by name and add to them the same phone (Notice that adding a phone is done ONLY via addPhone!)

  • remove the last contact (of the previous contacts from #8) by id check the size is updated

  • Enable this:

  • for(const contact of Phonebook){
  • //contacts will be delivered in alphabetical order
  • }
  • for(const contact of Phonebook.nameContains('someString')){
  • //contacts with 'cohen' will be delivered
  • }

About

This is a Typescript EX that practices Iterator-design-pattern.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published