Learn Node.js design patterns, The Iterator pattern

the iterator is a very common design matter it gives us a uniform interface for interacting with lists collections arrays or any type of aggregate object iterators are designed to work with collections of data they give us a clear way to access the first data record and then they give us a way to iterate through the rest of the rows within our data set it doesn't matter what type of data you're working with iterators give us a clear way to iterate through any collection the Gang of Four defines the intent of the iterator as providing a way to access the elements of an aggregate object sequentially without exposing its underlying representation let's take a look at some code within the exercise files under chapter 4 lesson 6 within the Start folder we have a couple of files already set up we have an inventory item J S which has the class definition for an inventory item inventory items just have a name and a price and they also have a write line method that will write that name and price to standard output we also have an index js5 and before we look at the code of this let's just go ahead and run it so that we can see what it does so if I run this file load index dot yes you can see that we see a message that says press any direction key now as I press the arrow keys we get to see what the last arrow key that I pressed was notice that we're not doing this with console log we're actually overriding the last thing that we've written to the console so that might be left or right or up or down let's go ahead and stop this application and come back to the code so within the index.jsp do include the inventory item class and we also set up an array of inventory items on line 8 we'll come back to that in a minute also on lines 3 & 4 we set up standard input to listen for key precedents through read line if we go down to line 19 we can see where we actually handle a key press of it and lines 21 and 22 clear the line and move the cursor to zero so that we can replace the text that was printed to the last line we're also checking the key name in a switch statement and each of these cases respond to a specific arrow key so right now all we're doing is writing right left down or up to standard output what we want to do is when the user presses right we want them to iterate to the next inventory item within this array if the user presses left we want them to iterate to the previous item and up should take us to the first item of the array and down should take us to the last item now clearly we know how to work with arrays well enough to actually just handle this directly in the handler we could save the current index to a variable and then use that to iterate through the array however an iterator provides a uniform interface for iterating through any collection so what we're going to do is we are going to create an iterator that we can use with this array of inventory items let's take a look at what I mean by adding some code to this file so what we're gonna do is we're gonna create iterator class and I'm going to import it here and then instead of saving inventory to an array we are actually going to make this an instance of our iterator class and now the iterator is going to give us a way to iterate through this array without having to worry about the mechanics of dealing with an array so let's go down to where we press the right key and instead of pressing the right key what we're gonna do is type inventory dot next the idea here is that inventory next we'll return one instance of an inventory item the next one that we're supposed to be looking at I'm going to go ahead and just chain on the right line call to this next because we have an inventory item we can use its write line method to go ahead and write the name and price of that item I'll go ahead and copy this line because when the user presses the left key will do something pretty similar instead of calling inventory next we'll just call inventory brief this will give us the previous inventory item we're gonna do the same thing for the down case so whenever the user presses the down arrow I will get the last inventory item and whenever you think user presses the up arrow I will go ahead and get the first inventory item so this is how it is are designed to make managing your collection any collection less complicated in the next lesson we will implement the iterator class and make this application work

Tags: , , , ,

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *