Java design pattern (2)------DAO pattern

Source: Internet
Author: User

java design mode (2)

First, DAO mode

1, in the Java program, often need to permanent data, or need to obtain permanent data, but there are many problems in such processes, such as: different data sources, different storage types, different suppliers. Access methods are different, and the use of DAO mode enables data persistence with a unified interface.

2. Solution:

3, DAO's understanding

①, DAO is actually using the combination Factory mode to solve the problem, and did not bring new functions, so learning is actually a train of thought.

②, DAO, theoretically, there is no limit to the number of layers.

The layers of ③ and DAO are theoretically not successively.

4, the nature of DAO pattern is a layer of shielding a change.

Second, the common design pattern application

1. Interface-Oriented programming

①, interface-oriented programming is the first major principle.
②, in Java programming, is very fastidious about the division of layers and the division of modules. Usually we divide the program according to three layers, respectively, the UI layer, the logical layer (named business or service), the data storage layer, they have to communicate through the interface.


design pattern of three-layer structure

③, in each layer, there are many small modules, a small module external should also be a whole, then a module external should also provide interface, other places need to use the function of this module, should pass this interface.

2. Parameters and return values in the interface definition

①, the interface must define the interface of the isolation part of the method to be exposed, for the method has parameters and the definition of the return value, in the end need to describe in the interface how to pass the parameters?

②, Value object mode gives us a good solution. In fact, the value object has become the standard method of data exchange between different layers or different modules, which embodies the encapsulation of the data and facilitates the reuse of the object.

Module (component) design diagram after adding Value object mode

3, programming, external how to get the interface

                     ①, we know, In a Java program, you use a class to get an instance of a class by using new, and then call the class's                 properties and methods through this class instance.
                     ②, but interface, how do we get an example of it? To know that interfaces are not directly new, the way we've learned it before is: Interface                    inf = new Implementation Class () ;
                     ③, but in an interface-oriented world, if you do this, it means that the client must know your specific implementation class, This destroys the isolation of the interface.                   The use of interfaces is not to let the outside know the implementation of the internal isolation. What do we do?
                     ④, the newly-learned factory model gives a solution to this problem, using factory to "produce" the interface , providing only factories and interfaces to the outside.


add value object and Factory mode module (component) design diagram

4, internal implementation of how to do data storage operations

Before we have learned how to design a module to the public part, then, in the internal implementation, when the need for data storage, what should we do? For this problem, the DAO pattern gives a better solution.

Module (component) design after adding value object, factory, and DAO mode


Three, component/module design thought Summary

1. Interface-oriented Programming: delamination, encapsulation isolation

2. Object mode: Pass parameter and return value

3, Factory mode: Get an example of the interface

4. DAO Mode: Internal, data storage

iv. Java Programming-Package Structure

1, a large part of the system architecture of Java programming is embodied in the package structure

2. The package structure of a typical Java application: prefix. The name of the app or project. Module Combination-Technical implementation within the module

Description

①, prefix: is the website domain name of the inverted write, remove www (for example, sun Company (non-JDK level) things: com.sun.*).

②, wherein the module combination is composed of systems, subsystems, modules, components, etc. (depending on the size of the project, if the project is large, then more than a few layers.)

③, the technical implementation of the module is generally composed of: presentation layer, logic layer, data layer and so on.

3, for many classes to use the public module or public class, you can create a separate package, named Common or base, these public classes are put in it, for the functional public module or public class can be established util or tool package, put into it.


V. Common ways of design and implementation, basic functions of DAO

1, the design time: from the big to the small

Break up a big problem into a series of small problems first. Or to break up a large system into smaller systems, the small system continues to decompose down until it is broken down into its own control, and then the hands-on implementation.

2, the realization of the time: from small to large

The component is implemented, the test is passed, several components are implemented into the composition module, the test passes, and then continues to expand.

3. The most typical DAO interface usually has the function:

New function, modify function, delete function, query according to main key value, get all value function, query according to condition function.

4. A generic DAO interface template

5, Uservo and Userqueryvo differences: Uservo encapsulates the data record, and Userqueryvo is used to encapsulate the query criteria.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java design pattern (2)------DAO pattern

Related Article

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.