Rajinder Menu

Explanation of HelloWorld example with Loose Coupling




This example is same as our HelloWorld example (without loose coupling) and prints HelloWorld on the console. In previous HelloWorld example we have seen how Spring used IOC design pattern in order to move the control for creating objects out of our application code to Spring framework. This example does the same thing but it also adds one more important design principle known as loosely coupled code.
Though this example is very trivial to show the benefits of loosely coupled code but still it will fulfil its purpose for the beginners.

Our example has four files:

 1) HelloInterface.java (An interface)

2) HelloWorld.java (Implementation of above interface)

3) MainApp.java (File containing main() function to execute the project)

4) ApplicationContext.xml ( File containing bean configurations)

In previous HelloWorld example we have seen that MainApp class is using HelloWorld class directly. It is directly dependent on HelloWorld class making our code tightly coupled with it.

public class MainApp{
public static void main(String args[]){
    ...
   HelloWorld helloBean=(HelloWorld)ac.getBean("hello");
  helloBean.sayHello();
 }
}



But in this example MainApp class is not using HelloWorld class directly. Instead it is using an interface HelloInterface. HelloWorld implements HelloInterface.

public class MainApp{
public static void main(String args[]){
    ...
   HelloInterface helloBean=(HelloInterface)ac.getBean("hello");
  helloBean.sayHello();
 }
}


Now as we have removed the direct dependency of MainApp from HelloWorld class, our code becomes loosely coupled.

Suppose there is another class SecondHelloWorld which also implements interface HelloInterface.

public class SecondHelloWorld implements HelloInterface{
  public void sayHello(){
   System.out.println("I am saying Hello again!");
 }

Above class is same as HelloWorld class but prints some different message.

Now if we want MainApp to use SecondHelloWorld class instead of HelloWorld class, all we have to do is just change bean configuration in ApplicationContext.xml like this: 

<beans .... >

   <bean id="hello" class="examples.spring.helloworld.SecondHelloWorld/>

</beans>


We have not touched our application code (MainApp) and our implementation of HelloInterface got changed.



That’s the beauty of loosely coupled code. Due to loosely coupled code we have removed the implementation details from our main application code. If later we want to change the implementation we can do that with minimum of changes.

Conclusion:

1)      Due to IOC we have moved creation of objects out of our code to configuration file. Thus freeing our code from the responsibility of creation and management of objects.

2)      Due to loose coupling we have removed implementation details out of our code.

3)      And since our application code does not depends on implementation and also does not creating objects using  new’ keyword, whenever we need to change the implementation all we have to do is to change the bean definition in configuration file (ApplicationContext.xml) only.
   
     Otherwise to change the implementation of HelloInterface we would have to change our application code of MainApp and recompile it like this :
         
      public class MainApp{
 
   public static void main(String args[]){
  
   HelloInterface  helloBean = new SecondHelloWorld(); 
       
   helloBean.sayHello();   
  
   }
}

  
    Changing in code like above in small example like this one, does not seems problematic but in large projects changing like this at several places matters!

 
  
 



I would like to know your comments and if you liked the article then please share it on social networking buttons.


No comments:

Post a Comment