1. Trang chủ
  2. » Giáo Dục - Đào Tạo

(Tiểu luận) design pattern essay data access object and builder pattern

26 5 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Data Access Object And Builder Pattern
Tác giả Phạm Thành Phong, Phạm Nguyễn Phát Đạt
Người hướng dẫn GV Nguyễn Thanh Phước
Trường học Ton Duc Thang University
Chuyên ngành Information Technology
Thể loại essay
Năm xuất bản 2023
Thành phố Ho Chi Minh City
Định dạng
Số trang 26
Dung lượng 2,62 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

VIETNAM GENERAL CONFEDERATION OF LABOUR TON DUC THANG UNIVERSITY FACULTY OF INFORMATION TECHNOLOGY DESIGN PATTERN ESSAY DATA ACCESS OBJECT AND BUILDER PATTERN... VIETNAM GENERAL CONFED

Trang 1

VIETNAM GENERAL CONFEDERATION OF LABOUR

TON DUC THANG UNIVERSITY FACULTY OF INFORMATION TECHNOLOGY

DESIGN PATTERN ESSAY

DATA ACCESS OBJECT AND BUILDER PATTERN

Trang 2

VIETNAM GENERAL CONFEDERATION OF LABOUR

TON DUC THANG UNIVERSITY FACULTY OF INFORMATION TECHNOLOGY

DESIGN PATTERN ESSAY

DATA ACCESS OBJECT AND BUILDER PATTERN

Trang 3

THANK YOU

First, we would like to express our sincere thanks and deep gratitude to Professor Nguyen Thanh Phuoc, who has always supported and guided us wholeheartedly during the research and completion of the report end of term

Next, we would like to thank the Department of Information Technology, Ton Duc Thang University for creating conditions for me to study and research this subject The faculty has always been ready to share useful knowledge as well as to share experiences of referencing documents, which helps not only in carrying out and completing research projects but also

in learning and training during practice at Ton Duc Thang University in general

Finally, after a period of studying in class, we completed the research project thanks to the guidance, help and knowledge learned from the teachers Due to the limitation of knowledge and reasoning ability, the group still has many shortcomings and limitations We would like to ask for your guidance and contributions to make our research more complete Moreover, thanks to the suggestions from teachers and friends, we will complete better in future research papers We hope that all of our teachers and friends - who always care about and support me - are always full of health and peace

WE THANK YOU!

Trang 4

PROJECT COMPLETED AT TON DUC THA NG

UNIVERSITY

I hereby declare that this is my own project and is guided by Mr Nguyen Thanh Phuoc The research contents and results in this topic are honest and have not been published in any form before The data in the tables for analysis, comments and evaluation are collected by the author himself from different sources, clearly stated in the reference section

In addition, the project also uses a number of comments, assessments as well

as data of other authors, other agencies and organizations, with citations and source annotations

If I find any fraud, I will take full responsibility for the content of my project Ton Duc Thang University is not related to copyright and copyright violations

caused by me during the implementation process (if any)

Trang 5

TEACHER'S CONFIRMATION AND ASSESSMENT SECTION The confirmation part of the instructor

_ _ _ _ _ _

Ho Chi Minh City, day month year

Author (Sign and write full name)

The evaluation part of the teacher marks the exam

_ _ _ _ _ _ _

Ho Chi Minh City, day month year

Author

Trang 6

SUMMARY

The report is made using the knowledge learned and materials provided by the lecturer, in addition to the reference of documents at Ton Duc Thang University Library

Trang 7

design and

200

Mtk CK - Môn design pattern sử dụng 9…

Trang 8

5

Table of Contents

THANK YOU i

PROJECT COMPLETED AT TON DUC THANG UNIVERSITY ii

SUMMARY 4

LIST OF TABLES, PICTURES, GRAPHS 6

CHAPTER 1 DATA ACCESS OBJECT PATTERN 7 –

- Introduction: 7

- Problem: 7

- Solution: 8

- Code example: 9

- Some popular libraries that apply the DAO pattern: 12

- Conclusion: 13

CHAPTER 2: BUILDER PATTERN 14

- Introduction: 14

- Problem: 14

- Solution: 15

- Code example: 16

- Some popular libraries apply builder pattern: 19

REFERENCES 21

design and

(28) - hi hello annyong

design and

1

Trang 9

LIST OF TABLES, PICTURES, GRAPHS

LIST OF PICTURES:

Picture 1 DAO architecture 9 Picture 2 Builder ructure 15St

Trang 10

object-The DAO pattern is widely used because it provides a high level of abstraction, which makes it easier to manage changes to the data source This is because the data access logic is isolated from the rest of the application, making it easier to update the code when the data source changes

In this report, we will explore the problem that DAO pattern solves, the solution that it provides, and provide an example implementation with code snippets

- Problem :

In many applications, data needs to be accessed and manipulated from different locations within the code This can lead to tight coupling between the domain model and the persistence layer, which makes it difficult to maintain and modify the codebase over time This is especially true when working with complex databases that require complex queries

Additionally, when an application uses multiple data sources or changes the data

Trang 11

This can lead to inconsistencies in the application's data and can make it difficult to scale the application as needed

The DAO pattern addresses these issues by providing a layer of abstraction between the application's domain model and the persistence layer This makes it easier to manage changes to the data source and helps to ensure that the application remains consistent over time

- Solution:

The DAO pattern provides a simple, easy- -use interface for accessing the todatabase This interface defines a set of methods for performing common CRUD (Create, Read, Update, Delete) operations on the data source The interface is implemented by one or more DAO classes, which are responsible for interacting with the database and performing the necessary operations

By using DAO classes, the application's domain model is decoupled from the persistence layer, which makes it easier to maintain and modify the codebase over time Additionally, because the DAO classes provide a consistent interface for accessing the data source, it is easier to manage changes to the data source and ensure that the application remains consistent over time

Trang 12

Picture 1: DAO architecture

In summary, the DAO pattern provides a layer of abstraction between the application's domain model and the persistence layer, which makes it easier to manage changes to the data source and helps to ensure that the application remains consistent over time

private Stringname;

private String email ;

// constructor, getters, setters, etc

}

Trang 13

Next, we would define a DAO interface to provide a set of methods for interacting with the data source In this example, we'll define a UserDao interface with methods for creating, reading, updating, and deleting users:

public interfaceUserDao{

publicvoidcreateUser(User user);

publicUsergetUserById(int id);

publicvoidupdateUser(User user);

publicvoiddeleteUser(User user);

}

Next, we would implement the UserDao interface using a concrete DAO class In this example, we'll use JDBC (Java Database Connectivity) to interact with a MySQL database:

public classUserJdbcDao implements UserDao{

private Connection connection ;

// constructor, initialize connection, etc

publicvoidcreateUser(User user){

String sql = "INSERT INTO users (name, email) VALUES (?, ?)";

PreparedStatement statement =

connection.prepareStatement(sql);

statement.setString(1, user.getName());

statement.setString(2, user.getEmail());

statement.executeUpdate();

statement.close();

}

publicUsergetUserById(int id){

String sql = "SELECT * FROM users WHERE id = ?";

Trang 14

PreparedStatement statement =

connection.prepareStatement(sql);

statement.setInt(1, id);

ResultSet result statement= executeQuery();

User user = null;

if(result.next()){

user = new User();

user.setId(result.getInt(" "));

user.setName(result.getString("name"));

user.setEmail(result.getString("email"));

publicvoidupdateUser(User user){

String sql = "UPDATE users SET name = ?, email = ? WHERE id =

?";

PreparedStatement statement =

connection.prepareStatement(sql);

statement.setString(1, user.getName());

statement.setString(2, user.getEmail());

statement.setInt(3, user.getId());

statement.executeUpdate();

statement.close();

}

publicvoiddeleteUser(User user){

String sql = "DELETE FROM users WHERE id = ?";

PreparedStatement statement =

connection.prepareStatement(sql);

Trang 15

statement.setInt(1, user.getId());

public classMain{

public static voidmain(String[] args) {

UserDao userDao = newUserJdbcDao();

User user = new User();

user.setName("Phat Dat");

user.setEmail("phatdat@gmail.com");

userDao.createUser(user);

}

}

This code would create a new UserJdbcDao instance and use it to create a new user

in the database with the name "Phat Dat" and email "phatdat@gmail.com"

- Some popular libraries that apply the DAO pattern:

Hibernate: Hibernate is a popular ORM (Object-Relational Mapping) framework

for Java It uses the DAO pattern to abstract the data access layer from the rest of the application

Spring Data: Spring Data is a part of the Spring Framework that provides a unified

data access API for working with different data stores, including databases, NoSQL stores, and message brokers It uses the DAO pattern to implement the data access layer

Trang 16

MyBatis: MyBatis is a data persistence framework that provides a SQL-based

approach to interacting with databases It uses the DAO pattern to provide a layer of abstraction between the application and the database

Django: Django is a popular Python web framework that includes an

Object-Relational Mapping (ORM) system for working with databases It uses the DAO pattern to separate the application logic from the database access layer

Ruby on Rails: Ruby on Rails is a popular web framework for Ruby It uses the

DAO pattern to provide a layer of abstraction between the application and the database

- Conclusion:

In conclusion, the DAO pattern provides a solution to the problem of managing database operations in an efficient and maintainable manner It separates the data access logic from the business logic and provides a layer of abstraction that simplifies the codebase and makes it more modular The DAO pattern allows developers to easily switch between different data storage mechanisms without affecting the rest of the application

In addition, the DAO pattern improves the testability of the codebase, making it easier to write unit tests and integration tests for the data access layer By encapsulating the data access logic, developers can mock or stub the DAO layer when writing tests, without worrying about the underlying database implementation

Overall, the DAO pattern is a widely-used design pattern in software development that provides many benefits in terms of code organization, maintainability, and testability By using this pattern, developers can write more robust and scalable applications that are easier to maintain and update over time

Trang 17

CHAPTER 2: BUILDER PATTERN

- Introduction:

The Builder Pattern is a creational design pattern that allows you to create complex objects step-by-step It is often used when you need to create an object with many possible configuration options, and you want to avoid the complexity of a large constructor with many parameters

With the Builder Pattern, you can separate the construction of an object from its representation, allowing you to create different representations of the same object This makes the code more flexible and easier to maintain

In this report, we will discuss the problem that the Builder Pattern solves, its solution, provide an example, and show some code implementations

- Problem:

When creating complex objects, you may encounter situations where you have to pass a large number of parameters to the constructor This can make the constructor difficult to use and maintain, especially if the object has many optional parameters

For example, imagine you are creating a car object The car has many optional features such as a sunroof, leather seats, a premium sound system, and more You could create a constructor with all of these optional parameters, but that would make the constructor unwieldy and hard to use Plus, if you ever need to add or remove optional features, you would have to change the constructor, which could cause issues if the constructor is used in many places throughout your code

This is where the Builder Pattern comes in It allows you to create an object in a

Trang 18

- Solution:

Picture 2: Builder Structure

In this diagram, the key classes and relationships involved in the Builder pattern are:

Client: The class that needs to create complex objects It collaborates with the

Trang 19

Director: The class that knows how to use the Builder interface to create complex objects It uses the Builder to construct objects in a step-by-step fashion, and only asks for the final product when it is ready

Builder: An interface that defines the steps to create a complex object This interface is implemented by the ConcreteBuilder

ConcreteBuilder: A class that implements the Builder interface It knows how to create and assemble the parts of the complex object, and it keeps track of the product it is building

Product: The complex object being built It has several parts, each of which may be constructed by the ConcreteBuilder

The solution to the problem of complex object creation using the Builder pattern involves creating a hierarchy of classes that work together to create complex objects

in a step-by-step fashion The key idea is to separate the construction of the object from its representation, so that the same construction process can create different representations This allows for greater flexibility and reuse, as different representations can be created by using different ConcreteBuilder classes that implement the same Builder interface

By using this pattern, we can avoid the complexity of having to create objects with many parameters, and instead use a step-by-step approach that is easier to read, understand, and maintain

- Code example:

Consider the construction of a home Home is the final end product (object) that is

to be returned as the output of the construction process It will have many steps like basement construction, wall construction, and so on roof construction Finally, the whole home object is returned Here using the same process, you can build houses with different properties

Ngày đăng: 19/12/2023, 15:18

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w