dao设计模式

DAO stands for Data Access Object. DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.

DAO代表数据访问对象。 DAO 设计模式用于在单独的层中分离数据持久性逻辑。 这样,该服务对如何完成访问数据库的低级操作一无所知。 这就是所谓的逻辑分离原理。

DAO设计模式 (DAO Design Pattern)

With DAO design pattern, we have following components on which our design depends:

使用DAO设计模式,我们具有设计所依赖的以下组件:

  • The model which is transferred from one layer to the other.从一层转移到另一层的模型。
  • The interfaces which provides a flexible design.提供灵活设计的接口 。
  • The interface implementation which is a concrete implementation of the persistence logic.接口实现是持久性逻辑的具体实现。

实施DAO模式 (Implementing DAO pattern)

With above mentioned components, let’s try to implement the DAO pattern. We will use 3 components here:

使用上述组件,让我们尝试实现DAO模式。 我们将在这里使用3个组件:

  1. The Book model which is transferred from one layer to the other.从一层转移到另一层的Book模型。
  2. The BookDao interface that provides a flexible design and API to implement.BookDao接口提供了灵活的设计和要实现的API。
  3. BookDaoImpl concrete class that is an implementation of the BookDao interface.BookDaoImpl具体类,它是BookDao接口的实现。

Let us put this logic into a diagram:

让我们将此逻辑放入图表中:

DAO模式模型类 (DAO Pattern model Class)

Now, let’s put up our model object.

现在,让我们建立模型对象。

package com.journaldev.model;public class Books {private int isbn;private String bookName;public Books() {}public Books(int isbn, String bookName) {this.isbn = isbn;this.bookName = bookName;}// getter setter methods
}

It is a simple object with just 2 properties to keep things simple.

这是一个简单的对象,只有2个属性,可以使事情保持简单。

DAO模式界面 (DAO Pattern Interface)

Let’s define the interface to access the data associated with it at persistence level.

让我们定义接口以持久性级别访问与其关联的数据。

package com.journaldev.dao;import com.journaldev.model.Books;import java.util.List;public interface BookDao {List<Books> getAllBooks();Books getBookByIsbn(int isbn);void saveBook(Books book);void deleteBook(Books book);
}

DAO模式实施 (DAO Pattern Implementation)

Next, we create a concrete class implementing the above interface.

接下来,我们创建一个实现上述接口的具体类。

package com.journaldev.daoimpl;import com.journaldev.dao.BookDao;
import com.journaldev.model.Books;import java.util.ArrayList;
import java.util.List;public class BookDaoImpl implements BookDao {//list is working as a databaseprivate List<Books> books;public BookDaoImpl() {books = new ArrayList<>();books.add(new Books(1, "Java"));books.add(new Books(2, "Python"));books.add(new Books(3, "Android"));}@Overridepublic List<Books> getAllBooks() {return books;}@Overridepublic Books getBookByIsbn(int isbn) {return books.get(isbn);}@Overridepublic void saveBook(Books book) {books.add(book);}@Overridepublic void deleteBook(Books book) {books.remove(book);}
}

使用DAO模式 (Using DAO Pattern)

Finally, we put this implementation to use in our main() method:

最后,我们在main()方法中使用此实现:

package com.journaldev;import com.journaldev.dao.BookDao;
import com.journaldev.daoimpl.BookDaoImpl;
import com.journaldev.model.Books;public class AccessBook {public static void main(String[] args) {BookDao bookDao = new BookDaoImpl();for (Books book : bookDao.getAllBooks()) {System.out.println("Book ISBN : " + book.getIsbn());}//update studentBooks book = bookDao.getAllBooks().get(1);book.setBookName("Algorithms");bookDao.saveBook(book);}
}

DAO模式的优势 (Advantages of DAO pattern)

There are many advantages for using DAO pattern. Let’s state some of them here:

使用DAO模式有很多优点。 让我们在这里声明其中一些:

  1. While changing a persistence mechanism, service layer doesn’t even have to know where the data comes from. For example, if you’re thinking of shifting from using MySQL to MongoDB, all changes are needed to be done in the DAO layer only.在更改持久性机制时,服务层甚至不必知道数据来自何处。 例如,如果您正在考虑从使用MySQL过渡到MongoDB,则所有更改仅需要在DAO层中完成。
  2. DAO pattern emphasis on the low coupling between different components of an application. So, the View layer have no dependency on DAO layer and only Service layer depends on it, even that with the interfaces and not from concrete implementation.DAO模式强调应用程序不同组件之间的低耦合。 因此,View层不依赖于DAO层,而仅Service层依赖于DAO层,即使依赖于接口,也不依赖于具体的实现。
  3. As the persistence logic is completely separate, it is much easier to write Unit tests for individual components. For example, if you’re using JUnit and Mockito for testing frameworks, it will be easy to mock the individual components of your application.由于持久性逻辑是完全独立的,因此为单个组件编写单元测试要容易得多。 例如,如果将JUnit和Mockito用于测试框架,则可以轻松模拟应用程序的各个组件。
  4. As we work with interfaces in DAO pattern, it also emphasizes the style of “work with interfaces instead of implementation” which is an excellent OOPs style of programming.当我们以DAO模式使用接口时,它还强调了“使用接口代替实现”的风格,这是一种出色的OOPs编程风格。

DAO模式结论 (DAO Pattern Conclusion)

In this article, we learned how we can put DAO design pattern to use to emphasize on keeping persistence logic separate and so, our components loosely coupled.

在本文中,我们学习了如何使用DAO设计模式来强调保持持久性逻辑分离,从而使我们的组件松散耦合。

Design patterns are just based on a way of programming and so, is language and framework independent. Feel free to leave your views in comments below. Download the DAO example project from below link.

设计模式仅基于编程方式,因此与语言和框架无关。 请随时在下面的评论中留下您的看法。 从下面的链接下载DAO示例项目。

Download DAO Pattern Example Project下载DAO模式示例项目

References: Oracle Documentation, Wikipedia.

参考: Oracle文档 , 维基百科 。

翻译自: https://www.journaldev.com/16813/dao-design-pattern

dao设计模式

dao设计模式_DAO设计模式相关推荐

  1. 设计模式(35)-----设计模式阶段性总结(一句话概括一个模式)

    假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情 ...

  2. 深入浅出设计模式之设计模式简介

    一.什么是设计模式(详见设计模式概念和七大原则) 在GoF(Gang of Four)的书籍<Design Patterns - Elements of Reusable Object-Orie ...

  3. 设计模式---适配器设计模式

    设计模式---适配器设计模式 什么事适配器: 1. 在使用监听的时候,需要定义一个类事件监听器接口 2. 通常接口中有多个方法,而程序中不一定所有的方法都用到,但又必须重写,很繁琐 3. 适配器简化了 ...

  4. 设计模式一の设计模式详解

    一.设计模式定义 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计模式的目的:为了代码可重用性.让代码更容易被他人理解.保证代码可靠性 ...

  5. 结构设计模式 - 适配器设计模式

    结构设计模式 - 适配器设计模式 PANKAJ  17评论 适配器设计模式是结构设计模式之一,其使用使得两个不相关的接口可以一起工作.连接这些不相关接口的对象称为适配器. 目录[ 隐藏 ] 1适配器设 ...

  6. 行为设计模式 - 解释器设计模式

    行为设计模式 - 解释器设计模式 解释器设计模式是行为设计模式之一.解释器模式用于定义语言的语法表示,并提供解释器来处理该语法. 解释器设计模式 解释器设计模式的最佳示例是java编译器,它将java ...

  7. 行为设计模式 - 迭代器设计模式

    行为设计模式 - 迭代器设计模式 迭代器设计模式中的一种行为模式.迭代器模式用于提供遍历一组对象的标准方法.Iterator模式广泛用于Java Collection Framework.Iterat ...

  8. 行为设计模式 - Memento设计模式

    行为设计模式 - Memento设计模式 Memento 设计模式是行为设计​​模式之一.当我们想要保存对象的状态以便稍后可以恢复时,使用Memento设计模式.Memento模式用于实现这一点,使得 ...

  9. 行为设计模式 - 状态设计模式

    行为设计模式 - 状态设计模式 状态设计模式是行为设计​​模式之一.当Object根据其内部状态更改其行为时,将使用状态设计模式. 目录[ 隐藏 ] 1国家设计模式 1.1状态设计模式接口 1.2国家 ...

最新文章

  1. R可视化使用ggplot2将坐标轴设置为对数坐标轴(Log Scale)
  2. 使用Eclipse更新软件时出现Unable to locate secure storage module错误的解决方法
  3. $.ligerdialog.open中确定按钮加事件_彻底搞懂JavaScript中的this指向问题
  4. (转)Javascript 面向对象编程(一):封装
  5. java中什么方法用来清空流_这个真的写的很细,JavaIO中的常用处理流,看完只有10%的人还不懂了...
  6. .Net Core小技巧 - Hosted Services + Quartz实现定时任务调度
  7. DevOps通用及版本控制面试题
  8. 语音识别现状与工程师必备技能
  9. 第三届IEEE普适媒体计算国际会议(U-Media 2010
  10. 关于Ubuntu 16.04系统挂载硬盘以及迁移MYSQL数据存储目录的操作步骤
  11. Linux列出安装过的程序
  12. 基于分布式光纤传感的高压电力线路异常监测探讨
  13. js/JavaScript获取IP地址的方法小结
  14. c预言plc编程语言,【讨论】对于PLC的编程语言STL、LAD、SCL等,你用的哪种语言呢?...
  15. html展开图片,Html 显示图片的三种方式
  16. IE设置每次打开时都清除缓存
  17. Python批量快速合并excel文件
  18. win10安装opencv,pycharm中import cv2失败
  19. 不只是技术!成为IT经理必备的十大软技能
  20. IDEA全局搜索快捷键方法

热门文章

  1. [转载] pandas入门:Series、DataFrame、Index基本操作都有了!
  2. html前端通过canvas生成验证码
  3. Cannot set property 'onclick' of null报错
  4. 正则,bs4 ,xpath 和jsonpath 的匹配规则
  5. 【分享】学长的安利来了~~O(∩_∩)O
  6. Java getResourceAsStream返回为空的问题
  7. [SHELL]判断一个命令是否存在
  8. 使用doxygen查看文件包含关系图
  9. cvCreateStructuringElementEx理解
  10. ADO.NET - Optimistic Pessimistic Concurrency