Loading...

Tuesday, June 30, 2015

Difference between Dependency Injection and Factory Pattern in Java

TL;DR Main difference between dependency injection and factory pattern is that in case of former dependency is provided by third party (framework or container) while in case of later dependency is acquired by client class itself. Another key difference between them is that use of dependency injection result in loosely coupled design but use of factory pattern create tight coupling between factory and classes which are dependent on product created by factory. Though both Dependency Injection and Factory pattern looks similar in a sense that both creates instance of a class, and also promotes interface driven programming rather than hard coding implementation class; But, there are some subtle differences between Factory pattern and dependency injection pattern. In case of factory design pattern, client class is responsible for calling getInstance() of factory class to create instance of products, it also means that client class is directly coupled with factory and can't be unit tested without factory class being available. On the other hand in Dependency Injection, client class has no clue about how his dependencies are created and managed. It only knows about dependencies. Mostly dependencies are injected by framework e.g. bean class exists without any hard coded dependency, as those are injected by IOC container e.g. Spring. You can also used points used here to answer questions like difference between Spring IOC and Factory pattern because Spring IOC is nothing but an implementation of dependency injection pattern. BTW, if you are serious about learning design patterns and principles, I suggest you to take a look at Head First Object Oriented Analysis and design book. This book is overshadowed by its popular cousin Head First Design Pattern but its one of the book to master object oriented design principles and patterns.

Saturday, June 27, 2015

2 Ways to check If String is Palindrome in Java? Recursion and Loop

A String is said to be Palindrome if it is equal to itself in reverse order. You can use this logic to check if String is Palindrome or not. There are two common ways to find if a given String is Palindrome or not in Java, first by using for loop, also known as iterative algorithm and second by using recursion, also known as recursive algorithm. The crux of this problem lies in how do you reverse String in Java? because once you have the String in reverse order, problem reduced to just comparing itself with the reversed String. If both are equal then given String is Palindrome otherwise it's not. Also whether your solution is iterative or recursive will also determine by implementing this logic. If you reverse String using for loop then it become an iterative solution and if you reverse String using recursion then it become a recursive solution. In general, recursive solution are short, readable and more intuitive but subject to StackOverFlowError and that's why not advised to be used in production system. You should always be using iterative solution in production, unless your programming language supports tail recursion optimization e.g. Scala which eliminates risk of StackOverFlowError by internally converting a recursive solution to an iterative one. If you are doing this exercise as part of your Interview preparation then I suggest you to take a look at Cracking the Coding Interview: 150 Programming Questions and Solutions, as title says it contains 150 good questions based upon different topics e.g. String, array, linked list, binary tree, networking etc. A good book for preparing both Java and C++ interview.

Friday, June 26, 2015

java.lang.ClassNotFoundException: org.postgresql.Driver - Cause and Solution

java.lang.ClassNotFoundException: org.postgresql.Driver error comes when you are trying to connect to a PostgreSQL database from Java program but Java ClassLoader is not able to find the Driver class "org.postgresql.Driver" required to make the connection. Usually this class is find in PostgreSQL JDBC driver JAR e.g. postgresql-9.4-1201.jdbc41.jar, which is required to connect PostgreSQL server version greater than 9.3 from JDK 1.7 or JDK 1.8, the exact JAR depends upon your PostgreSQL server version, the Java version you are running and JDBC version your are using. Now your problem could be either you don't have that PostgreSQL JDBC driver JAR in your machine or the JAR is not in your classpath, or you might be battling with some classpath intricacies. If you don't have this JAR, then solution of "java.lang.ClassNotFoundException: org.postgresql.Driver" is simple, just download it from PostgreSQL site. Make sure you download the correct version of JDBC driver based upon the PostgreSQL server you are connecting and JVM version of your machine. Once you download that, just put in the lib directory of your Java application e.g. WEB-INF/lib if you are connecting to PostgreSQL from Java Web Application. If you are running in Tomcat, then you can alternatively also put this in tomcat/lib directory, but beware of the difference in the application dependency in WEB-INF/lib or in tomcat/lib. This error is exactly similar to java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver, which comes when you try to connect to Oracle database and driver's JAR is not in CLASSPATH. Steps and approach mentioned there also application to solve this problem.