Node js Design Pattern, Chain of responsibility

Think about ordering a sandwich from your favorite sub shop. The first thing that you might be asked to select is the bread. Then you might have to select the meat. Followed by the cheeses. Followed by the vegetables. And finally to the condiments. You’re sandwich gets made on an assembly line. The steps that go into making that sandwich are ordered.

And you can claim the sandwich at any point in the process. If you only want meats and cheeses, then the sandwich doesn’t need to complete the rest of the steps on line. You can just take it and eat it. This analogy is similar to the Chain of Responsibility design pattern.

This pattern allows us to chain together objects to handle a request. A request is sent to an object to handle it and then that handler could process the request and return a result, or it could pass the request on to the next handler.

Which could return a result or pass the request on to the next handler. All the way down the chain until you get to the last handler, which will always return some sort of result. The Gang of Four defines the intent of the Chain of Responsibility as, avoid coupling the sender of a request and its receiver by giving more than one object a chance to handle the request.

Chain the receiving objects and pass the request along the chain until an object handles it.Let’s take a look at some code that we can help improve off the Chain of Responsibility design pattern. I’m looking at our exercise files in Chapter four. Chapter four, lesson one. And within the start folder we already have some files. So take a look at the index.js file. So we are using the Store and we are pulling our inventory data from an adjacent document. On line four, when we instantiate a ski shop, we send it the inventory that’s on the floor at the store.So we can search for an item like ski hats.

If I come over here to the terminal to run this, node index.js, we can see that we find that itemand that we have 15 of ’em. The inventory is actually coming from the inventory json data document. And you can see that we have more options than just the floor. We have the floor, the back room, a local store, and the warehouse. So if a user wants to find an inventory item,we should actually search all of these places. For instance, if I change our index to search for ski poles and then run the application, I get an undefined. Meaning we can’t find ski poles on the floor, but we haven’t searched the other locations.

We can modify the Store to use the Chain of Responsibility design pattern to improve the Store’s find method. Let’s go ahead and take a look at how that would work. First, in the index.js, we need to send the entire inventory catalog to the store, not just the items on the floor. And now, we’re sending the entire catalog of inventory that’s available to the ski store to the actual Store instance. In the Store, I’m going to import a new class called Storage. So now in the Store’s constructor, as opposed to just setting up an inventory for the store, we are going to set up several storage locations using our new objects.

We’ll have the floor, and we’ll pass it the inventory that is found on the floor. We’re also going to have a back room, and we’ll pass it the inventory for the back room. That’s all lower case.We also have a local store that we can search if we cannot find you’re item on the floor or in the back room. And then finally, we have a warehouse. Which is the last location we will search for an item and if we can’t find the item there, then the store simply doesn’t carry it.

Now the thing about getting inventory items from a local store or a warehouse, is that might take some time. So I’m going to add another argument here, which is the time and days it might take your inventory item to reach our store if it’s found in another store. That will only take one day to get it here from a local store. We’re going to assume that it will take five daysto get it to this store from the warehouse if we happen to find the item there. So now we’ll set this storage, the storage for this store, to the floor. So that’s the first place we’re going to look for items. And then I can take the floor and call set next and set the next place to look for items, which would be the back room.

And I can take the back room and call set next, which will be the next place we’ll look for items, which will be the local store. And the local store we can set next on this object to look in the warehouse. And the next thing that we want to do is modify the find method. So instead of returning an item from this store’s inventory, what we’re going to try to do is find that item in storage. So we will return item name. Great, so this is how we can use the Chain of Responsibility design pattern.

The first request will be piped through several storage objects until the inventory item is found. In the next lesson, we’ll go ahead and implement the code that’s necessary for the storage class.


Please enter your comment!
Please enter your name here