Saturday, November 20, 2010

A day at Thought Works !!!

I did not know about this company before August 2010. I have heard this name before, but i thought it is yet another software company that exists in this IT world. I was wrong then. This company is really a pioneer in following Agile methodologies. It really has a strange procedure for conducting its interviews and for selecting its employees. I am here to narrate about what had happened to me in the past when i took up an interview with Thought works.

I applied for a Java based Engineer position at thought works and i did not get a call for a long time. I thought it is a place for the geeks and I forgot about this. One day i got a call from Thought works chennai asking me about my interest with Thought works and how i came to know about the opening and what do i know about thought works. I spoke to that guy for about 10 minutes and he assessed me and my interests towards TW. He said he will send an email with the assignment and asked me to resend it within three days with the source code for the problem and the unit testing code as well. I received an email from him asking me to solve one of the two common problems (they generally used to provide), the first one being Mars Rovers and the second one is the Sales Tax Problem.

I chose the Mars Rovers problem and solved it in my way. I had applied a couple of design patterns and sent the code to him.

I got the feedback after 2 weeks saying that I have been short listed in the first round and they asked me to come for the second round of interviews to their office venue near guindy.

My second round was a pair programming round and I had a senior guy ( i guess ) and one girl who sat with me to analyse the pros and cons in my coding style and my OOPS logic. We analysed the code for about half hour and found few modifications to be done based on refactoring concepts. A long discussion went on between me and the senior guy about exposing a couple of methods. The senior guy did not look compromised as i went ahead with my solution to solve the problem (This is the root cause for my rejection :-(). Once we were done with this, they gave me an extension to solve. As i had solved the Mars Rovers problem, the extension was given regarding the same. He told me that whenever a rover moves to the edge of the plateau, it dies and it marks that place with a beacon before that, so that no other rovers in future will move to that place. He asked me to implement that. I was able to complete that.

After this i had a panel interview and that went on for about an hour and those guys asked me every nook and corner in all the projects that i worked on and also the technologies involved in that. Then they gave me a small design and asked me to draw the class diagram for that issue. I was able to complete that with little suggestions provided by them.

After two days, I was conveyed a message that i did not make it to the final round of the interview. The discussion i had with the senior guy did not work out and that had led to my rejection.

So People, whoever is interested or applying for Thought works try to be well versed with the OOPS concepts and design patterns and refactoring patterns. Even if you know all this, please do not argue with any of the tech panel members. If you do this, i think anyone can make it to the "Geeks company" (That s how they call themselves).

All the best !!!!

Spring core and differences between constructor and setter injection..

There may be lots and lots of Spring tutorials online, but i thought writing a blog like this would be helpful for someone to understand the core concepts of Spring with small examples. People please feel free to pass your comments, so that i can resolve the same in my future technical blogs.

Instead of giving too many theoretical explanations, i have chosen to give a lot of practical examples so that people would find it pretty easy to grasp the core concepts of Spring. We are not going to discuss about the Aspect Oriented Programming support in Spring. This blog covers only the core concepts of Spring. The examples have been given just to make a novice understand about the different concepts of Spring. These classes may not be 100 % object oriented.

Now, let s move into the topic.

Spring is a light weight framework that addresses each tier in a web based application. It provides support for Dependency Injection in the business layer, Spring MVC in the presentation layer and it provides template support for the ORM frameworks like Hibernate, Ibatis etc.,.

Spring framework comprises of several modules. The below given image explains the different modules available as part of the Spring framework. Spring core forms the base for all these modules.

Spring core has two important concepts.

1. Dependency Injection.
2. Aspect Oriented Programming.

What is Dependency Injection???

Instead of objects instantiating other objects, the dependent objects are added by the Spring IOC (Inversion Of Control or Dependency Injection) container based on XML configuration. This happens at run time. It promotes loose coupling and thus prevents hard coding of object creation or lookup.

Now let us look at an example for this.

In this example, this Player class has two instance variables which are meant for holding the player name and the game which that player plays. Here in this case, the Player is meant only for playing Cricket and there is no possibility to change the type of the game he can play. This is one simple example for Tight coupling.







How to avoid this using Spring ?????

Spring supports a good programming practice called "Programming to an Interface". Such a practice makes sure that any subtype of the interface reference can be injected at run time by the container. What we are going to do is to re design the above mentioned classes using this practice to promote loose coupling.

Have a look at the below given examples.











The Player class has been redesigned in such a way that the previous "Cricket" instance variable reference has been replaced by "Game" interface reference so that we can inject any of "Game" interface subtype to the "Player" instance during run time. Now let us see how dependency injection for this example is handled using Spring.

The process of Dependency Injection can be carried out by using Spring in two ways. One by using the setter methods of instance variables and the other is by using Constructors. The configuration XML for setter method based injection is as follows

An instance of a class has to be declared in the XML using the tag. Here in this case, we have created instances for the classes Cricket and Hockey. The tag has attributes like id, class, name etc,. The id attribute will be used to fetch the instance of the bean during run time from the container. The class attribute will be used to specify the class type of the instance that is going to be created. The tag is a child of tag and it will be used to map the values or references to the instance variables of the pertaining class the bean tag is referring to. The tag attribute name will be used to refer the instance variables that particular class has, the value attribute will be used to assign a literal and ref attribute will be used to assign references to the instance variables. In this case, an instance of the Player class will be created and the container will assign a value "Dhanraj Pillai" for the playerName instance variable and assigns a reference of hockey for the game. The classes Hockey and Cricket implements the Game interface, so the container maps the hockey bean reference to the game instance variable of the Player class. Hope this makes things clear about how to promote loose coupling using Spring.

The configuration XML for constructor based injection for the same scenario is as follows.

The configuration changes between the setter method based injection and constructor based injection is minimal. This configuration XML expects the player class to have a constructor with two parameters, one for the playerName and the other one for the type of game the player will play. We are injecting Sachin as the name of the Player and Cricket as the game he can play.


This marks the end of the explanation for the difference between the constructor based injection and setter method based injection.

Catch u guys in the next post...