Pages

Saturday, 10 October 2015

Advantages of Hibernate over JDBC

Hibernate, Powerful ORM solution to map Java classes to database tables. It can be done using XML mapping files. In JDBC, it is developer’s responsibility to handle JDBC result set and convert it to Java objects done manually.

Hibernate supports caching mechanism which reduce the number of round trips between an application and the database automatically, which result to application performance. But in JDBC, caching is maintained by hand-coding.

Hibernate provided Dialect classes, so we no need to write SQL queries in hibernate, instead we use the methods provided by that API.

Hibernate has its own query language HQL (hibernate query language) which is database independent. If we want to change the database, we need to update the Dialect only. But in case of JDBC, we need to update all SQL queries.

This will also supports collections like List, Set, and Map (Only new collections).

Hibernate only have Un-checked exceptions, so no need to write try, catch, or no need to write throws.  Actually in hibernate we have the translator which converts checked to Un-checked. But in JDBC, all exceptions are checked exceptions, so we must write code in try, catch and throws.

Getting pagination in hibernate is quite simple.

Hibernate has capability to generate primary keys automatically while we are storing the records into database.

Hibernate, if it will not found any table in the database while inserting any record this will create the table. But in case of JDBC will raise an error like “View not exists”, and throws exception.

Monday, 5 October 2015

Prim's Algorithm

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.

For graphs that are sufficiently dense, Prim's algorithm can be made to run in linear time, meeting or improving the time bounds for other algorithms.

1.  Start at any node in the graph.
Mark the starting node as reached.
Mark all the other nodes in the graph as unreached.

#Minimum cost Spanning Tree (MST) consists of the starting node.

2. Find an edge e with minimum cost in the graph that connects a reached node x to an unreached node y.

3. Add the edge e found in the previous step to the MST.
Mark the unreached node y as reached.

4. Repeat the steps 2 and 3 until all nodes in the graph have become reached.


Pseudo code

ReachSet = {0};                    // You can use any node...
UnReachSet = {1, 2, ..., N-1};
SpanningTree = {};

while ( UnReachSet ≠ empty ) {
              Find edge e = (x, y) such that:
                    x ∈ ReachSet
                    y ∈ UnReachSet
                    e has smallest cost

              SpanningTree = SpanningTree ∪ {e};
              ReachSet   = ReachSet ∪ {y};
              UnReachSet = UnReachSet - {y};
}

Do you know it?


Developed by: Czech mathematician Vojtěch Jarník in 1930.

Rediscovered and republished by: computer scientists Robert C. Prim in 1957 and Edsger W. Dijkstra in 1959.

Factory Method Pattern (Virtual Constructor)

When we want to return one sub-class object from multiple sub-classes using an input, should use Factory design pattern. Factory class takes responsibility of instantiation the class (We can return Singleton instance from static factory method).

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.



       

Example:Coffee/Vending machine, give input from options and as per input coffee, lemon tea, plain milk or hot water will be an output.

interface Drink {
       void prepare();
}

class Coffee implements Drink {
       @Override
       public void prepare() {
              System.out.println("Coffee is prepared !!");
       }
}

class LemonTea implements Drink {
       @Override
       public void prepare() {
              System.out.println("Lemon Tea is prepared !!");
       }
}

class PlainWater implements Drink {
       @Override
       public void prepare() {
              System.out.println("Plain Water is prepared !!");
       }
}

class VedingMachine {
       public static Drink getDrink(String str) {
              if("PlainWater".equals(str)) {
                     return new PlainWater();
              } else if("Coffee".equals(str)) {
                     return new Coffee();
              } else if("LemonTea".equals(str)) {
                     return new LemonTea();
              }
              return null;
       }
}

public class FactoryPatternTest {
       public static void main(String[] args) {
              Drink drink = VedingMachine.getDrink("Coffee");
              drink.prepare();
       }
}

Output: Coffee is prepared !!


Benefits of Factory Method Pattern

Factory Method Pattern provides approach to code for interface rather than implementation and it provides abstraction between implementation and client classes through inheritance.

Factory Method Pattern allows the sub-classes to choose the type of objects to create.

We can easily change the implementation of sub-class because client program is unaware of this. It makes code more robust, less coupled and easy to extend (client interacts solely with the resultant interface or abstract class).

Usage in JDK

java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods uses Factory pattern.

valueOf() method in wrapper classes like Boolean, Integer etc.

Spring and hibernate frameworks.



Factory Method Pattern (Virtual Constructor)

Factory Method Pattern (Virtual Constructor)

When we want to return one sub-class object from multiple sub-classes using an input, should use Factory design pattern. Factory class takes responsibility of instantiation the class (We can return Singleton instance from static factory method).

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.



       

Example:Vending machine, give input from options and as per input coffee, lemon tea, plain milk or hot water will be an output.

interface Drink {
       void prepare();
}

class Coffee implements Drink {
       @Override
       public void prepare() {
              System.out.println("Coffee is prepared !!");
       }
}

class LemonTea implements Drink {
       @Override
       public void prepare() {
              System.out.println("Lemon Tea is prepared !!");
       }
}

class PlainWater implements Drink {
       @Override
       public void prepare() {
              System.out.println("Plain Water is prepared !!");
       }
}

class VedingMachine {
       public static Drink getDrink(String str) {
              if("PlainWater".equals(str)) {
                     return new PlainWater();
              } else if("Coffee".equals(str)) {
                     return new Coffee();
              } else if("LemonTea".equals(str)) {
                     return new LemonTea();
              }
              return null;
       }
}

public class FactoryPatternTest {
       public static void main(String[] args) {
              Drink drink = VedingMachine.getDrink("Coffee");
              drink.prepare();
       }
}

Output: Coffee is prepared !!


Benefits of Factory Method Pattern

Factory Method Pattern provides approach to code for interface rather than implementation and it provides abstraction between implementation and client classes through inheritance.

Factory Method Pattern allows the sub-classes to choose the type of objects to create.

We can easily change the implementation of sub-class because client program is unaware of this. It makes code more robust, less coupled and easy to extend (client interacts solely with the resultant interface or abstract class).

Usage in JDK

java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods uses Factory pattern.

valueOf() method in wrapper classes like Boolean, Integer etc.

Spring and hibernate frameworks.