This is my blog , it could contain various topics from my own thoughts to computer programming ....

Sunday, March 10, 2013

Going Technical...Iterator design pattern

It has been quite sometime since I wrote the last post on this blog. In fact I tried few times to write something new but due to lack of time I was not able to do that. Also I was unable to find a simple yet elegant real life analogy to explain the "Iterator" design pattern.

Anyways now that I have found an analogy which may be good enough for this. Let's see whether you can grasp the idea without much effort.

We all are familiar with multistory buildings. Usually these buildings have some sort of fire alarming systems. When an accidental fire occurs these systems alert the occupants of the building so that they could move to a safe place. In addition to that when such fire alarming happens there may be an evacuation procedure to follow.Once the building occupants have moved to a safe ground someone would be in charge of making sure that all the occupants have arrived to the safe ground.Now that let's see how we could use this analogy to understand "Iterator" pattern.

"Iterator" design pattern is used when there is a need to inspect every single element in a group of elements and to do something with those. Usually this group of elements belongs to another parent data structure and when there is a need to inspect the elements in the group one has to request it from the parent data structure. The parent data structure is not supposed to reveal its internal information yet it has to allow inspection of all the elements

Well above paragraph tells us what Iterator design pattern is supposed to do. It is time to see how this fits to our analogy. Our analogy has a multistory building , occupants , fire alarming system and an evacuation procedure. The multistory building can be seen as the parent data structure. Occupants can be seen as the group of elements. Fire alarming system and evacuation procedure can be seen as the way how elements are exposed to outside for some sort of processing. When the evacuation procedure is followed occupants are supposed to take the stair case and walk to the safe ground one after the other.