Essential

 If I give you an assignment to design Shopping cart web application, how will you define the architecture of this application. You are free to choose any framework, tool or server?
Ans: Usually I will choose a MVC framework which will make me use other design patterns like Front Controller, Business Delegate, Service Locater, DAO, DTO, Loose Coupling etc. Struts 2 is very easy to configure and comes with other plugins like Tiles, Velocity and Validator etc. The architecture of Struts becomes the architecture of my application with various actions and corresponding JSP pages in place.

Why is it better to use hibernate than JDBC for database interaction in various Java applications?
Ans: Hibernate provides an OO view of the database by mapping the various classes to the database tables. This helps in thinking in terms of the OO language then in RDBMS terms and hence increases productivity.

What is the purpose of intern() method in the String class?
Ans: It helps in moving the normal string objects to move to the String literal pool.

What is Thread in JAVA?
– A thread refers to a single sequential flow of control within a program. A thread is an independent path of
execution within a program. Threads allow the program to perform multiple tasks simultaneously.

What is  Bubble Sorting ?
– Bubble Sorting is an algorithm in which we are comparing first two values and put the larger one at higher index. Then we take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again  from zero index up to n-1 index. The algorithm follows the same steps iteratively until elements are not sorted.

List of Design Pattern :-
* Factory Design Pattern
* Abstract Factory Design Pattern
* Builder Design Pattern
* Prototype Design Pattern
* Singleton Design Pattern
* Adapter Design Pattern
* Bridge Design Pattern
* Composite Design Pattern
* Decorator Design Pattern
* Facade Design Pattern
* Flyweight Design Pattern
* Proxy Design Pattern
* Mediator Design Pattern
* Memento Design Pattern
* Interpreter Design Pattern
* Iterator Design Pattern
* COR Design Pattern
* Command Design Pattern
* State Design Pattern
* Strategy Design Pattern
* Observer Design Pattern
* Template Design Pattern
* Visitor Design Pattern
* Dependency IOC Design pattern


Reference URL

Database Interaction with DAO and DTO Design Patterns
DAO means Data Access Object and is an industry design pattern. In this article, I will discuss What is a DAO and how to create a DAO. I will also discuss what is the significance of creating Data Access Objects.
There are tools which can generate DAO code for an application. But one can also write DAO classes manually. Also one more design pattern very closely related to DAO is the DTO (Data Transfer Object).

A DAO design pattern helps an application to perform various CRUD operations on the database. The DAO classes provide methods for insertion, deletion, updation and finder methods. The basic purpose of creating DAO is the loose coupling and non-repeation of code.

In any application which is going to interact with the database, there is need of performing CRUD operations on the database tables and since the table operations may be done by different classes and hence it becomes cumbersome to repeat the same code in various classes. Moreover, even after repeating the code, it becomes difficult to maintain the datbase interaction code whenever changes are required in the way database interaction is being done.

To overcome the above mentioned problems, the DAO design pattern was invented. The DAO pattern is supposed to have a DAO interface, class and factory corresponding to each table in the table. For example if there is a table name Employees in the database then a interface named EmployeesDAO, a class named EmployeesDAOImpl and a factory class EmployeeDAOFactory is supposed to be in place.

The EmployeesDAO interface should look something like:

package com.company.app.dao;
public interface
{
	public Employee[] findAll() throws SQLException;
	public Employee findByPK(EmployeePK) throws SQL Exception;
	public Employee[] findbyemployeename(String EmployeeName)
	throws SQLException;
	public boolean insert(Employee employee) throws SQLException;
	public boolean update(Employee employee) throws SQLException;
	public boolean delete(Employee employee) throws SQLException;
}

The DAO Implementation class looks like:

package com.company.aap.doaimpl;
public class EmployeeDAOImpl implements EmployeeDAO
{
    Connection con;
    ResultSet rs;
    PreparedStatement stmt;
    public Employee[] findAll() throws SQLException 
	{
        Employee[] employees;
        String SQL_QUERY= “Select * from Employee”;
        con=ResourceManager.getConnection();
        stmt = con.prepareStatement(SQL_QUERY);
        rs = stmt.executeQuery();
		   while(rs.next) 
		   {
				//get columns and store in array
		   }
		   return employees;
    }

    public Employee findByPK(EmployeePK) throws SQL Exception 
    {
       //Implementation code
    }
    public Employee[] findbyemployeename(String EmployeeName) throws SQLException
	{
       //Implementation code
    }
    public boolean insert(Employee employee) throws SQLException
	{
        //Implementation code
    }
    public boolean update(Employee employee) throws SQLException
	{
        //Implementation code
    }
    public boolean delete(Employee employee) throws SQLException
	{
        //Implementation code
    }

}

In the above DAO implementation code, the Employee class is the data transfer object which has member variables corresponding to the Employee table in the database. Thus Employee is a POJO in a sense with getters and setters for the member variables.

The DAO Factory class which is EmployeeDAOFactory is used for returning the object of EmployeeDAOImpl.
Here is the code for DAO facrory for the EmployeeDAO:

package com.company.app.factory;
public class EmployeeDAOFactory
{
      public static EmployeeDAO create()
      {
           return (new EmployeeDAOImpl());
      }

}

In the EmployeeDAOImpl there is a ResourceManager class being used whose purpose is to create database connection and return it back. This class thus acts as a helper class for various other DAO implementation classes.

As I mentioned in the start of this article that there are tools which can generate the DAO, DTO and Factory class code for you. These tools are helpful because they automate the generation of DAO classes. The importance of these tools even becomes great in the initial phase of application development because the database schema keeps of changes frequently and hence manually making the changes in database tables and the DAO code. Here I am listing the tools which can generate the DAO code for the Java applications:

1) FireStrom DAO by Codefutures
2) DaoGen by TitanicLinux.net
3) Dao4J
4) DB Visual Architect

For example;.
http://titaniclinux.net/daogen
http://titaniclinux.net/cms/FC?link=daogen-examples

What these tools do is import the tables from the database by asking the connection details and then generate the DAO/DTO classes based on the tables being imported. One more important point that is useful when using these tools is that they keep a check on the primary and foreign keys of the table structure. Moreover, the database datatypes are converted into corresponding datatypes in Java in the DAO/DTO classes.

After the code has been generated by these tools, one can always modify the code to optimize the DAO code.

Always remember that the DAO, DTO, Factory, Loose Coupling (A SOLID principle), Factory design patterns all go along. One can also create a connection pool while using the DAO design pattern. The important point is that the code becomes clean and easy to understand when using the DAO, DTO design patterns.


Reference URL :

The Adapter pattern is used so that two unrelated interfaces can work together. The joining between them is called an Adapter. This is something like we convert interface of one class into interface expected by the client. We do that using an Adapter.

Let’s try and understand this with the help of an example. Again, I will like to take a general example. We all have electric sockets in our houses of different sizes and shapes. I will take an example of a socket of 15 Ampere. This is a bigger socket and the other one which is smaller is of 5 Ampere. A 15 Amp plug cannot fit into a 5 Amp socket. Here, we will use an Adapter. The adapter can be called a connector here. The connector connects both of these and gives output to the client plug which is of 5 Amp.

The Adapter is something like this. It will be having the plug of suitable for 15 Amp and a socket suitable for a 5 Amp plug. So, that the 5 Amp plug which here is the client can fit in and also the server which here is the 15 Amp socket can give the output.

Let’s try and convert the same example into a software program. How do we do this? Let’s try and understand the problem once more. We have a 5 Amp plug and want a 5 Amp socket so that it can work. We DO NOT have a 5 Amp socket, what we have is a 15 Amp socket in which the 5 Amp plug cannot fit. The problem is how to cater to the client without changing the plug or socket.


Reference URL for more helps http://sharat.wordpress.com


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s