Tuesday, February 11, 2014

The Data Access Object (DAO) Design Pattern

In this post, we'll see the basic of DAO Design Pattern, how to implement and what advantages it provide and it's use.


Suppose you are at Disney land with your family or friends and you decide to take a ride on carousel. You see a big panel with so many buttons for operating the carousel.You ask the operator to start the carousel, adjusts it speed and stop it. The operator who know how to use panel, follows your instructions. He is providing you abstraction from the complicated operating panel. In fact, if you go to different carousel in other fair, you can instruct its operartor in the same way and the operator will follow your instructions in the same way even thought his panel is different from that of the first carousel. In essence, you and your family enjoy can take a ride on any carousel without understanding its operating panel because the knowledge to operate the machine is abstracted by the operator.
The Data Access Object pattern provide you abstraction in the same as carousel operator does in providing to their customers.






















In real-life projects, you will encounter situation in which you want to make your data persist. You might use flat files, XML files, RDBMS etc. In such situations, you can use DAO design pattern. This design pattern abstract the details of the underlying persistence mechanism whether you use mysql or oracle and offers you an easy-to-use interface for implementing the storing the data. The DAO pattern hides the implementation detail of the data source from its clients, thereby introducing loose coupling between your core business logs and your storage mechanism.
This will help you to move from one type store to another storage mechanism.

Let’s examine the structure of the pattern.


Apart from the above classes (Client, DAO, TransferObject and DataSource), there could be one more class for this pattern - DAOFactory. You may have multiple DAO object corresponding to different types of objects you want to store such as XML, MYSQL, ORACLE. This factory will define one method for each DAO object.

Let's see with the help of example what we have studied above. In this Paint example, we have a circle class that you want to store in persistance data store. For this we'll create a CircleTransfer object with setter and getter methods.
/*  
  * Transfer Object as per diagram  
  */  
 public class CircleTransfer{  
      private int x;  
      private int y;  
      private int radius;  
      void setX(int x){  
           this.x=x;  
      }  
      void setY(int y){  
           this.y=y;  
      }  
      int getX(){  
           return x;  
      }  
      int getY(){  
           return y;  
      }  
      void setRadius(int radius){  
           this.radius=radius;  
      }  
      int getRadius(){  
           return radius;  
      }  
 }  
 /*  
  * DAO as per Diagram  
  */  
 interface CircleDAO{  
      public void insertCircle(CircleTransfer circle);  
      public CircleTransfer findCircle(int x,int y);  
      public void deleteCircle(int x,int y);  
 }  
 /**  
  * DataSource as per Diagram  
  * Oracle Implementation  
  */  
 public class RDMSDAO implements CircleDAO{  
      @Override  
      public void insertCircle(CircleTransfer circle) {  
           // insertCircle implementation as per Oracle database  
      }  
      @Override  
      public CircleTransfer findCircle(int x, int y) {  
           // findCircle implementation  
           return null;  
      }  
      @Override  
      public void deleteCircle(int x, int y) {  
           // deleteCircle implemenation  
      }  
 }  
 /**  
  * DataSource as per Diagram  
  * MySql Implementation  
  */  
 public class MYSQLDAO implements CircleDAO{  
      @Override  
      public void insertCircle(CircleTransfer circle) {  
           // insertCircle implementation as per MYSQL database  
      }  
      @Override  
      public CircleTransfer findCircle(int x, int y) {  
           // findCircle implementation  
           return null;  
      }  
      @Override  
      public void deleteCircle(int x, int y) {  
           // deleteCircle implemenation  
      }       
 }  
 /*  
  * Factory   
  */  
 public class DAOFactory{  
      public static CircleDAO getCircleDao(String sourceType){  
           switch(sourceType){  
           case "Oracle":  
                return new RDMSDAO();  
           case "mysql":  
                return new MYSQLDAO();  
           }  
           return null;  
      }  
 }  
 /*  
  * Core Business logic  
  */  
 public class Circle{  
      private int x,y,r;  
      Circle(int x,int y, int r){  
           this.x=x;  
           this.y=y;  
           this.r=r;  
      }  
      public CircleTransfer getCircleTransferObject(){  
           CircleTransfer c = new CircleTransfer();  
           c.setRadius(r);  
           c.setX(x);  
           c.setY(y);  
           return c;  
      }  
 }  
 /*  
  * Client  
  */  
 public class DaoPatternDemo {  
      public static void main(String[] javalatte) {  
           Circle c = new Circle(2,2,4);  
           CircleTransfer ct = c.getCircleTransferObject();  
           CircleDAO cDao = DAOFactory.getCircleDao("oracle");  
           cDao.insertCircle(ct);  
      }  
 }  
The Circle class belongs to your core business logic, the Circle class contains a method—getCircleTransferObject()—that returns the CircleTransfer object with the required data.
You define the CircleDAO interface with three methods commonly used with data sources.
The RDBMSDAO implements CircleDAO with a concrete implementation to access the RDBMS data source.
The CircleTransfer object plays a data carrier role between the main() method (which is acting as a Client) and DAO implementation (i.e., the RDBMSDAO class).

Here are the benefits of the DAO design pattern:
  • The pattern introduces an abstraction: the DAO hides the implementation details of the actual data source from the core business logic. The business logic need not know about the nitty-gritty of the data source, which results in easy-to-understand, less complicated code.
  • The pattern separates the persistence mechanism from rest of the application code and puts it together in one class specific to one data source. This centralization enables easier maintenance and easier bug-tracing.
  • It is quite easy to extend support for other data sources using this pattern. For instance, if you want to provide support for the XML-based repository in the FunPaint application, this can be achieved by defining a new class (say XMLDAO). This new class will implement your CircleDAO interface, such that you do not need to change the way you access the data source. The only thing that needs to be changed is the parameter you pass to DAOFactory to create a DAO.



If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

No comments:

Post a Comment