Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Data Access Object Pattern

Data Access Object Pattern

The Data Access Object Pattern, or DAO Pattern, is used to separate low-level data access APIs or operations from high-level business services. The following are the participants of the Data Access Object Pattern.


Summary

Intent

Separate the data access logic from the business logic and encapsulate data access operations in a dedicated class.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Separation of concerns: separates the data access logic from the business logic, reducing the coupling of the system.
  2. Easy to Maintain: data access logic is centrally managed, making it easy to maintain and update.
  3. Extensibility: when the data source is changed or the data access logic is modified, the business logic layer is not affected.

Disadvantages

Usage recommendation

Notes

Main roles included

  1. DAO Interface:

  2. Declares the methods for data access and manipulation.
  3. - Declares the methods for data access and manipulation.
    1. DAO Implementation:

    2. Implements the DAO interface and encapsulates all access logic to the data source.
    3. - Implements the DAO interface and encapsulates all access logic to the data source.
      1. Data Transfer Object (DTO) (optional):

      2. Encapsulates the data retrieved from the data source for data transfer between the business logic layer and the data access layer.
      3. - Encapsulates the data retrieved from the data source for data transfer between the business logic layer and the data access layer.
        1. Business Logic Layer (Business Logic Layer):

        2. Interact with the data source through the DAO interface, without depending directly on the concrete implementation of data access.
        3. - Interact with the data source through the DAO interface, without depending directly on the concrete implementation of data access.
          1. Data Source:

          2. A database or other persistent storage that the DAO implementation interacts with to store and retrieve data.
          3. - A database or other persistent storage that the DAO implementation interacts with to store and retrieve data.

            The Data Access Object Pattern helps keep an application clean and manageable by providing a clear data access layer.


            Create a class that implements

            We will create a class that acts as the model object or value object. Student object.StudentDao is the Data Access Object interface.StudentDaoImpl is the concrete class that implements the Data Access Object interface.DaoPatternDemo, and our demo class uses the StudentDao to demonstrate the use of the Data Access Object Pattern.

            Step 1

            Create the value object.

            Student.java

            public

            class

            Student

            {

            private

            String

            name

            ;

            private

            int

            rollNo

            ;

            Student

            (

            String

            name

            ,

            int

            rollNo

            )

            {

            this

            .

            name

            =

            name

            ;

            this

            .

            rollNo

            =

            rollNo

            ;

            }

            public

            String

            getName

            (

            )

            {

            return

            name

            ;

            }

            public

            void

            setName

            (

            String

            name

            )

            {

            this

            .

            name

            =

            name

            ;

            }

            public

            int

            getRollNo

            (

            )

            {

            return

            rollNo

            ;

            }

            public

            void

            setRollNo

            (

            int

            rollNo

            )

            {

            this

            .

            rollNo

            =

            rollNo

            ;

            }

            }

            Step 2

            Create the Data Access Object interface.

            StudentDao.java

            import

            java

            .

            util

            .

            List

            ;

            public

            interface

            StudentDao

            {

            public

            List

            <
            
            Student
            
            getAllStudents
            
            (
            
            )

            ;

            public

            Student

            getStudent

            (

            int

            rollNo

            )

            ;

            public

            void

            updateStudent

            (

            Student

            student

            )

            ;

            public

            void

            deleteStudent

            (

            Student

            student

            )

            ;

            }

            Step 3

            Create the concrete class that implements the interface above.

            StudentDaoImpl.java

            import

            java

            .

            util

            .

            ArrayList

            ;

            import

            java

            .

            util

            .

            List

            ;

            public

            class

            StudentDaoImpl

            implements

            StudentDao

            {

            //

            The list is treated as a database

            List

            <
            
            Student
            
            students

            ;

            public

            StudentDaoImpl

            (

            )

            {

            students

            =

            new

            ArrayList

            <
            
            Student
            
            (
            
            )

            ;

            Student

            student1

            =

            new

            Student

            (

            "

            Robert

            "

            ,

            0

            )

            ;

            Student

            student2

            =

            new

            Student

            (

            "

            John

            "

            ,

            1

            )

            ;

            students

            .

            add

            (

            student1

            )

            ;

            students

            .

            add

            (

            student2

            )

            ;

            }

            @

            Override

            public

            void

            deleteStudent

            (

            Student

            student

            )

            {

            students

            .

            remove

            (

            student

            .

            getRollNo

            (

            )

            )

            ;

            System

            .

            out

            .

            println

            (

            "

            Student: Roll No

            "

            student

            .

            getRollNo

            (

            )

            "

            , deleted from database

            "

            )

            ;

            }

            //

            Retrieve the list of students from the database

            @

            Override

            public

            List

            <
            
            Student
            
            getAllStudents
            
            (
            
            )
            
            {
            
            return
            
            students

            ;

            }

            @

            Override

            public

            Student

            getStudent

            (

            int

            rollNo

            )

            {

            return

            students

            .

            get

            (

            rollNo

            )

            ;

            }

            @

            Override

            public

            void

            updateStudent

            (

            Student

            student

            )

            {

            students

            .

            get

            (

            student

            .

            getRollNo

            (

            )

            )

            .

            setName

            (

            student

            .

            getName

            (

            )

            )

            ;

            System

            .

            out

            .

            println

            (

            "

            Student: Roll No

            "

            student

            .

            getRollNo

            (

            )

            "

            , updated in the database

            "

            )

            ;

            }

            }

            Step 4

            Use StudentDao to demonstrate the use of the Data Access Object Pattern.

            DaoPatternDemo.java

            public

            class

            DaoPatternDemo

            {

            public

            static

            void

            main

            (

            String

            [

            ]

            args

            )

            {

            StudentDao

            studentDao

            =

            new

            StudentDaoImpl

            (

            )

            ;

            //

            Output all students

            for

            (

            Student

            student

            :

            studentDao

            .

            getAllStudents

            (

            )

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Student: [RollNo :

            "

            student

            .

            getRollNo

            (

            )

            "

            , Name :

            "

            student

            .

            getName

            (

            )

            "

            ]

            "

            )

            ;

            }

            //

            Update a student

            Student

            student

            =

            studentDao

            .

            getAllStudents

            (

            )

            .

            get

            (

            0

            )

            ;

            student

            .

            setName

            (

            "

            Michael

            "

            )

            ;

            studentDao

            .

            updateStudent

            (

            student

            )

            ;

            //

            Get a student

            studentDao

            .

            getStudent

            (

            0

            )

            ;

            System

            .

            out

            .

            println

            (

            "

            Student: [RollNo :

            "

            student

            .

            getRollNo

            (

            )

            "

            , Name :

            "

            student

            .

            getName

            (

            )

            "

            ]

            "

            )

            ;

            }

            }

            Step 5

            Run the program, output result:

            
            
            Student: [RollNo : 0, Name : Robert ]
            
            Student: [RollNo : 1, Name : John ]
            
            Student: Roll No 0, updated in the database
            
            Student: [RollNo : 0, Name : Michael ]