Design mode introduction and its application examples DAO design pattern implementation and analysis

Traditional software application systems generally adopt a three-tier application framework. The business logic layer code is mixed with various database call statements, which seriously affects the scalability, reusability and maintainability of the system.

There are many difficulties in designing reusable object-oriented software. If you find related objects; classify them at the appropriate granularity; define the interface and inheritance hierarchy of the class, establish the basic relationship between the objects; be targeted to the current problem, and have sufficient generality for future problems and needs Sex; avoid repetitive design or minimize repetitive design.

The design pattern solves these difficulties effectively, making it easy to reuse successful designs and architectures. By adopting the design mode, the system's scalability, reusability and maintainability can be greatly improved, and the system development difficulty can be reduced and the development efficiency can be improved. The design pattern has become a hot spot in the field of software engineering research, and is considered to be another major breakthrough after OOP technology.

Firstly, the design pattern is briefly introduced, then the advantages and disadvantages of the traditional 3-layer architecture development model are analyzed. The scalability, reusability and maintainability of the system are fully considered. The improvement method is proposed from the perspective of software design pattern, and a research example is given.

2 design mode

Each pattern describes a problem that is constantly recurring and the core of the solution to the problem. This allows the program to be used multiple times without having to repeat the work. The design pattern is the knowledge and experience recorded in the object-oriented software design process, with a series of class structures and objects to describe its meaning. The design pattern reuses object-oriented design solutions to more easily and easily reuse successful designs and architectures. Demonstrating proven technologies into design patterns also makes it easier for new system developers to understand their design ideas. Design patterns can help designers make choices that facilitate system reuse, avoiding damage to system reusability, and design patterns can even improve existing systems by providing an explicit class and object interaction relationship and a description of potential connections between them. Document management and system maintenance effectiveness. The design pattern determines which classes and instances are included and their roles, how they work, and how they are assigned. By characterizing the static and dynamic structure of components and their cooperative relationship, the design pattern is successfully applied to solve software structures such as commercial data processing, electronic communication, graphical user interface, database, and distributed communication software.

3 traditional 3-layer architecture development model

Currently, in the Internet/Intranet environment, enterprise-level application software systems mostly use a three-tier application framework: the presentation layer, the business logic layer, and the data layer (Figure 1). In the software framework of this hierarchy. Each layer provides services (service providers) to its upper layer and serves as its next level of customers (service consumers). The internal layers are only visible to adjacent layers, thus forming a portability and scalability. Sexual compatibility platform.

Application pattern design in business logic layer

However, there are also significant shortcomings: in the process of developing multiple application software systems, the coupling between different application software systems is not very good; the code between layers is confusing; the way to access the database is different, such as JDBC, Hibernate or JDO, therefore, porting between various databases requires a lot of changes, the business logic layer needs to be modified, and a consistent programming model cannot be adopted. The system's reusability and maintainability are not ideal.

4 Improved 4-layer architecture development model

Based on the above analysis, in order to improve the development efficiency of software, from the perspective of design mode, it is proposed to further separate the business logic layer and form a data interface layer. The data interface layer shields the differences between the various underlying databases and is responsible for the connection to the underlying database. Form a 4-layer software architecture framework, from top to bottom: presentation layer, business logic layer, data interface layer, data layer, as shown in Figure 2. The presentation layer is an interface for application software to perform human-computer interaction; the business logic layer is responsible for processing user's business requests; the data interface layer is responsible for interaction with the underlying database; and the data layer is responsible for storing data.

4.1 DAO design pattern

The data interface layer adopts a data access object DAO (Data Access Ob-iect) mode. This mode is actually a mixture of Adapter mode and Bridge mode. DAO objects provide basic operations for database access, such as adding, deleting, modifying, and querying. The DAO layer encapsulates database operations in an object-oriented manner. The DAO component is completely focused on the data access implementation, and the business layer code does not need to care about the implementation of the underlying database access, thereby reducing the coupling between the layers.

Advantages of the DAO design pattern:

(1) The DAO pattern abstracts the data access mode, and the business logic layer does not feel the existence of the data source when accessing the data source. There is an important rule in software factories: the less an object knows about other objects, the less it knows, the less dependencies and the higher the reusability.

(2) DAO concentrates data access on a separate layer, because all data access is handled by DAO. This layer of independent DAO strips off the implementation of data access and the rest of the system, concentrating data access, making the system more Maintainability.

(3) DAO reduces the complexity of the business logic layer. DAO simplifies the business logic layer by managing complex data access. All code related to the implementation of data access (such as SOL language, etc.) is not written in the business logic layer, the business logic layer can focus on business logic, improving the readability and productivity of the code.

(4) DAO helps to improve the portability of the system. The DAO pattern separates the implementation details of data usage and data access by dividing data access into an abstraction layer and an implementation layer. This means that the business layer has nothing to do with the underlying details of data access. That is to say, the specific mechanism of data access can be modified by switching the underlying implementation while keeping the upper layer unchanged, and the reusability of the system is improved.

(5) The DAO component relies on the database system to provide a database access interface. As long as the database is not reconstructed, the DAO layer usually does not need to be rewritten. The DAO layer transparently separates the database from the business logic layer, and the business logic layer focuses on the implementation of business logic without having to care about the implementation of persistence layer access.

(6) Further improvement of the DAO mode. Since the DAO layer has implemented all data access, the business logic layer only needs to call the DAO interface, so the business logic layer wraps the DAO using the Facade pattern. In order to achieve cross-database platform migration and support configurable switching between different data access mechanisms, it is necessary to introduce Factorv mode, Proxy mode and Strategy mode in the DAO layer, which can be easily switched between different data storage modes. However, with the DAO mode, the system adds a layer between the requesting data end and the data server, which increases the complexity of the system; the newly added layer requires additional design and implementation, which increases the workload; The abstract factory adds complexity to the design. In general, DAO separates the data persistence layer from the business logic layer, improving software scalability, maintainability, and reusability. 4.2 DAO implementation

The DAO object is also. Java objects are just the ability to provide database access. Database access can be summarized into four basic operations: Create, Query, Update, and Delete, which are commonly referred to as CRUD operations. The DAO mode is usually used with the factory pattern. It is recommended to interface-oriented programming. Write an interface for each DAO implementation class. The DAO caller uses the interface instead of the concrete implementation class. Of course, DAO objects require value objects to pass values, and value objects are ordinary JavaBeans. The following is an example of a DAO. Figure 3 shows the participating objects of the data access object design pattern and the calling relationship between them. Figure 4 is a detailed class diagram of the example.

Application pattern design in business logic layer

The DAO example includes files: DAO interface class, DAO interface implementation class, DaoFactory class, PersonBean class, DBConn class, Test class. The definition of the DAO object in the interface must provide a method, the PersonBean is a normal JavaBean, and the implementation class of the DAO object provides an implementation for all methods of the interface. The tool class DBConn is also used in the program. The tool class is mainly used to obtain the database connection, obtain the Statement object through the connection, and provide the method of releasing the Statement object and closing the connection.

The program designs the DBConn object into a singleton mode. At this point, the complete DAO instance is written. The program mainly provides three components: the JavaBean class that passes the value, the interface of the DAO object, the implementation class of the DAO object, and the implementation class that includes the tool class used. The DAO model is usually combined with the factory model, and the DAO factory is responsible for generating DAO instances. The combination of the two can better decouple the business components from the persistence layer components. The business component only needs to get the DAO factory instance, and then the DAO factory instance is responsible for generating the DAO component. The business component is oriented to the DAO interface programming, and does not need to care about the specific implementation of DAO.

5 Conclusion

Analyze the advantages and disadvantages of the traditional 3-layer software architecture, improve it from the perspective of software model, propose a 4-layer development model, improve the scalability, reusability and maintainability of the system, and give a specific The application example implements the DAO design pattern.

Ceshi Xiaojuzizhi

xiaojuzizhi

Ganzhou Green days Biochemical Technology Shower folder mechanic Co., Ltd. , https://www.cn-gangdao.com