Data Access Object Pattern - GeeksforGeeks

02 Dec, 2021
Data Access Object Pattern or DAO pattern is used to separate low-level data accessing API or operations from high-level business services. Following are the participants in Data Access Object Pattern. 
UML Diagram Data Access Object Pattern
Advantages
1. The advantage of using data access objects is the relatively simple and rigorous separation between two important parts of an application that can but should not know anything of each other, and which can be expected to evolve frequently and independently.
2. If we need to change the underlying persistence mechanism we only have to change the DAO layer and not all the places in the domain logic where the DAO layer is used from.
Disadvantages 
1. Potential disadvantages of using DAO is a leaky abstraction, code duplication, and abstraction inversion.
Design components
BusinessObject: The BusinessObject represents the data client. It is the object that requires access to the data source to obtain and store data. A BusinessObject may be implemented as a session bean, entity bean, or some other Java object in addition to a servlet or helper bean that accesses the data source.
DataAccessObject: The DataAccessObject is the primary object of this pattern. The DataAccessObject abstracts the underlying data access implementation for the BusinessObject to enable transparent access to the data source.
DataSource: This represents a data source implementation. A data source could be a database such as an RDBMS, OODBMS, XML repository, flat file system, and so forth. A data source can also be another system service or some kind of repository.
TransferObject: This represents a Transfer Object used as a data carrier. The DataAccessObject may use a Transfer Object to return data to the client. The DataAccessObject may also receive the data from the client in a Transfer Object to update the data in the data source.
Example:
Java
// Java program to illustrate Data Access Object Pattern // Importing required classesimport java.util.ArrayList;import java.util.List; // Class 1// Helper classclass Developer {     private String name;    private int DeveloperId;     // Constructor of Developer class    Developer(String name, int DeveloperId)    {         // This keyword refers to current instance itself        this.name = name;        this.DeveloperId = DeveloperId;    }     // Method 1    public String getName() { return name; }     // Method 2    public void setName(String name) { this.name = name; }     // Method 3    public int getDeveloperId() { return DeveloperId; }     // Method 4    public void setDeveloperId(int DeveloperId)    {        this.DeveloperId = DeveloperId;    }} // Interfaceinterface DeveloperDao {    public List<Developer> getAllDevelopers();    public Developer getDeveloper(int DeveloperId);    public void updateDeveloper(Developer Developer);    public void deleteDeveloper(Developer Developer);} // Class 2// Implementing above defined interfaceclass DeveloperDaoImpl implements DeveloperDao {     List<Developer> Developers;     // Method 1    public DeveloperDaoImpl()    {        Developers = new ArrayList<Developer>();        Developer Developer1 = new Developer("Kushagra", 0);        Developer Developer2 = new Developer("Vikram", 1);        Developers.add(Developer1);        Developers.add(Developer2);    }     // Method 2    @Override    public void deleteDeveloper(Developer Developer)    {        Developers.remove(Developer.getDeveloperId());        System.out.println("DeveloperId "                           + Developer.getDeveloperId()                           + ", deleted from database");    }     // Method 3    @Override public List<Developer> getAllDevelopers()    {        return Developers;    }     // Method 4    @Override public Developer getDeveloper(int DeveloperId)    {        return Developers.get(DeveloperId);    }     // Method 5    @Override    public void updateDeveloper(Developer Developer)    {        Developers.get(Developer.getDeveloperId())            .setName(Developer.getName());        System.out.println("DeveloperId "                           + Developer.getDeveloperId()                           + ", updated in the database");    }} // Class 3// DaoPatternDemoclass GFG {     // Main driver method    public static void main(String[] args)    {         DeveloperDao DeveloperDao = new DeveloperDaoImpl();         for (Developer Developer :             DeveloperDao.getAllDevelopers()) {            System.out.println("DeveloperId : "                               + Developer.getDeveloperId()                               + ", Name : "                               + Developer.getName());        }         Developer Developer            = DeveloperDao.getAllDevelopers().get(0);         Developer.setName("Lokesh");        DeveloperDao.updateDeveloper(Developer);         DeveloperDao.getDeveloper(0);        System.out.println(            "DeveloperId : " + Developer.getDeveloperId()            + ", Name : " + Developer.getName());    }}
DeveloperId : 0, Name : Kushagra
DeveloperId : 1, Name : Vikram
DeveloperId 0, updated in the database
DeveloperId : 0, Name : Lokesh
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
solankimayank
rkbhola5
Design Pattern
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Command Pattern
Difference between Sequence diagram and Collaboration diagram
Observer Pattern | Set 2 (Implementation)
Template Method Design Pattern
Strategy Pattern | Set 1 (Introduction)
State Design Pattern
Difference between Sequence Diagram and Activity Diagram
Memento design pattern
Iterator Pattern
JDBC Using Model Object and Singleton Class