Implementing object storage with Command mode--object and relational database

Source: Internet
Author: User
Tags abstract table name

I. Development background

Object-oriented method is more and more important in the analysis, design and coding of software development, and it has some advantages that can not be compared with other development methods in adapting to the change of system requirements, improving the reusability of software and developing efficiency. Object-oriented idea describes the concepts in the application domain as objects, the application system consists of a series of objects, the objects can transmit messages, and the operation of the system can be said to be the cooperative work among the objects. People who have developed experience know that some objects in the application system are persisted and need to be saved to disk so that they can be transferred to the system when the system is restarted. These objects are primarily entity objects in object-oriented methods, and for consistency, the entity object represents all the objects that need to be stored in this article.

At present, there are two kinds of object storage methods: One is to save the file, the other is to deposit the database. The object in the file, easy to implement, simple to operate, there are many class libraries have implemented this function, but the file storage method is difficult to express the relationship between the objects, performance is also insufficient, difficult to meet the requirements of large systems. The ideal choice is object-oriented database, but the object-oriented database is still immature and cannot meet the need. After years of development of relational database system, technology has been quite mature and widely used, and most information systems have been used as background data management. Today, there are many mature database products, in order to reduce the difficulty in database programming, a variety of database access models have been published, such as ADO, ODBC, BDE, ADO. NET and JDBC, in spite of this, relational database is not to adapt to object-oriented technology and the existence and development of object-oriented language and relational database can not achieve direct object access, needs to be transformed, in order to improve the development efficiency, reduce the coupling between the application system and the database, in the second is necessary to develop object storage modules between the users.

Second, the implementation strategy

Database features. Database consists of data tables, in object-oriented programming, data tables generally represent entity objects, and fields in tables represent attributes of entity objects. Because the data table is a special data structure, the number of fields per table, field type and length, and other constraints are not the same, so it is difficult to establish a class that can unify the structure of each data table. Although the data table is different in structure, but the data tables have the same: they all have a name, the table has a common operation, such as query, add, delete, modify and other access operations.

The mapping between the entity object and the data table. For the characteristics of the database structure, we set up an abstract base class for all the entity objects, the class has the entity object name, the database access operation interface, each interface takes the database connection object as the parameter. Entity objects can inherit from abstract base classes, increase the assignment and read methods of related properties and attributes, and implement database access interfaces inherited from abstract base classes. The implementation strategy of the database access interface is to read the entity object name as the data table name, set the recordset with the data access model, assign the value of the field in the recordset to the property of the entity object, or save the property value into the datasheet. This establishes a corresponding relationship between the entity object and the data table.

Command mode. After the relationship between the entity object and the data table is established, it cannot exist in isolation, cannot be created and stored by itself, and must be organized by some mechanism to provide consistent service to the application system and interact with other objects. This functionality can be accomplished using the command mode in design mode. The command mode structure is shown in Figure 1.

Figure 1: Command design pattern

⑴. Command mode solution description.

The command abstract class describes the interfaces supported by all specific commands (Concretecommand), and the specific commands (Concretecommand) encapsulate the services used by the recipient (Receiver). The customer creates a specific command (Concretecommand) and binds these specific commands (Concretecommand) to the specified receiver (Receiver), and the caller (Invoker) actually executes a command or cancels the execution of a command.

⑵. Command mode conclusion:

① Command object and command algorithm are separated;

The ② Caller (invoker) is separated from the specified command and protected;

③ specific commands (Concretecommand) are objects that can be created and stored;

④ the new Specific command (Concretecommand) can be added without the need to modify the code.

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.