Showing posts with label DI. Show all posts
Showing posts with label DI. Show all posts

Monday, February 24, 2014

For my High Schooler: Inversion of Control


At dining table.

Yash: In programming class, today we have lot of discussion.

Me: About what?

Yash: Teacher was saying that Inversion of control is completely swept the programming land scape. It has changed the way, Java programs are written.  She explained Inversion of Control but most of the students were not able to understand the concept.

Me: hmm…

Yash: But you know, I know what is IoC?

Me: Explain to me.

Yash: It is very simple. In IoC, we try to separate What and When.

Me: I didn’t get it.

Yash: In a program, we have What part and When part. IoC states that separate What from When.

Me: Explain.

Yash: Let me think of an example.

Me: ok.

Yash: I saw you working on very old computer in our garage. In one of the screen, program was asking your name. You entered your name. Then it asked your address. You entered address.  In the end it asked you to save the entered value. Here What and When are tightly coupled.  You cannot enter address without entering name. This is an example of with IoC.

Me: Nice example.

Yash: It is not complete yet. It is now turn of example with IoC. Now think of today’s web page. Suppose you want to create an account with yahoo mail. This page will ask your name, address, phone number, and other things. On this page you can enter your name, address, phone number, etc. in any order.  Here What and When are separated as far as this page is concerned.

Me: Fantastic. You certainly deserve some reward. What about Ice cream.

Monday, December 24, 2012

Dependency Injection: For my Ninth Grader

Homestead High’s Astronomy Club has planned to arrange a visit to Foothill Observatory. So each student was asked to reach observatory at 9.00 AM on following Saturday. Everyone agreed. On Saturday, only quarter of the club members made to the observatory.

Club’s presiding officers were surprised that why only few students made to the observatory despite being so close to school. They decided to find out the reason, so future visits can have more participation. After talking to club members following reasons were listed for low attendance:

1. Non availability of public transport near observatory
2. Parents were busy somewhere else so no ride was available
3. Lack of coordination among students, so ride share was not common
4. …
5. ….
6. …

After few months, Club proposed to visit observatory again. But to avoid previous mistake, this time Club Officers arranged transport from School to observatory and back. Attendance rose to almost 100%.

If you notice in both scenarios only one thing has changed but results were drastically different. What was the change!!

In first scenario, each student was supposed to arrange his transport and in second scenario club officers had responsibility to arrange transport.

In second scenario students were dependent on club officers. It was up to club officers to arrange transport from First Student Charter or US Coachways any other transport company. It was possible that club officers might have arranged one bus from First Student Charter and second from US Coachways. In general students were dependent upon club officers for transport so have more time and energy to focus on learning.

This is Dependency Injection (DI).