"Design mode" Data Access object mode

Source: Internet
Author: User

The data Access object pattern, or the DAO pattern, is used to separate low-level data access APIs or operations from advanced business services. The following are participants in the data Access object pattern.

    • data Access Object Interface (Interface) -this interface defines the standard operations to be performed on a model object.
    • The data Access Object entity class (concrete Class) -This class implements the interface described above. This class is responsible for getting data from a data source, which can be a database, or XML, or other storage mechanism.
    • model Object/value Object/Numeric Object -The object is a simple POJO that contains the Get/set method to store the data retrieved by using the DAO class.
Realize

We will create a Student object as a model object or a numeric object. Studentdao is the data Access object interface. Studentdaoimpl is an entity class that implements the interface of a data access object. Daopatterndemo, our demo class uses Studentdao to demonstrate the use of the data Access object pattern.

Step 1

Creates a numeric object.

 Public classStudent {PrivateString name; Private intRollno; Student (String name,intRollno) {       This. Name =name;  This. Rollno =Rollno; }    PublicString GetName () {returnname; }    Public voidsetName (String name) { This. Name =name; }    Public intGetrollno () {returnRollno; }    Public voidSetrollno (intRollno) {       This. Rollno =Rollno; }}
Step 2

Creates a data Access object interface.

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 an entity class that implements the above interfaces.

Importjava.util.ArrayList;Importjava.util.List; Public classStudentdaoimplImplementsStudentdao {//list is treated as a databaseList<student>students;  PublicStudentdaoimpl () {Students=NewArraylist<student>(); Student Student1=NewStudent ("Robert", 0); Student Student2=NewStudent ("John", 1);      Students.add (STUDENT1);           Students.add (Student2); } @Override Public voiddeletestudent (Student Student) {students.remove (Student.getrollno ()); System.out.println ("Student:roll No" +Student.getrollno ()+ ", deleted from database"); }   //Retrieving student lists from the database@Override PublicList<student>getallstudents () {returnstudents; } @Override PublicStudent Getstudent (intRollno) {      returnStudents.get (Rollno); } @Override Public voidupdatestudent (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.

 Public classDaopatterndemo { Public Static voidMain (string[] args) {Studentdao Studentdao=NewStudentdaoimpl (); //output of all students       for(Student student:studentDao.getAllStudents ()) {System.out.println ("Student: [Rollno:" +student.getrollno () + ", Name:" +student.getname () + "]"); }      //Update StudentStudent Student =studentdao.getallstudents (). Get (0); Student.setname ("Michael");      Studentdao.updatestudent (student); //Get StudentsStudentdao.getstudent (0); System.out.println ("Student: [Rollno:" +student.getrollno () + ", Name:" +student.getname () + "]"); }}
Step 5

Verify the output.

Student: [rollno:0100, Name:michael]

"Design mode" Data Access object mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.