Use VFI to improve the reusability of Delphi programs
Abstract: Program reusability is one of the important indicators of software quality. Improving reusability has an important impact on reducing potential defects of programs, improving program development efficiency, and reducing maintenance costs. This article provides examples of specific application of VFI to improve program reusability for VFI provided by Delphi. This example shows that using VFI can greatly simplify program code, maintain the consistency of the interface, and improve program development efficiency.
Keywords: reusability, Delphi, VFI
1 Introduction
Software Reuse has been deeply rooted in people's hearts, and the idea of reusable libraries was proposed as early as 1968 at the NATO Software Engineering Conference. Software reuse, also known as software reuse or software reuse, has many definitions. One of the more authoritative and general ones is: software reuse is the process of creating a new software system using pre-established software department products. This definition contains two aspects that must be included in software reuse:
1. Systematically develop reusable software products. These soft parts can be code, but should not be limited to code, but can also be analysis, design, test data, prototype, plan, document, template, framework, etc.
2. These soft parts are systematically used as building modules to build new systems.
Software reuse can bring many benefits, such as improving software generation rate, shortening development cycles, reducing software development and maintenance costs, producing more standardized software, improving software development quality, and enhancing the interoperability of software systems.
Today, when object-oriented technology has become the mainstream technology in software development today, software reuse has been raised to an important position. Reusability is an object-oriented goal, and on the other hand, object-oriented technology provides a better means for software reuse. It improves the level of software reuse from relatively low-level reuse such as common source code reuse and library function reuse to class reuse, component reuse, and so on.
2 VFI in Delphi
As an object-oriented development tool, Delphi adopts Object Pascal as its language and provides a visual development environment, greatly improving the efficiency of software development.
Similar to common RAD tools such as Visual Basic, C++ Builder, Power Builder, etc., Delphi provides a form designer. What makes Delphi's form designer unique is that Delphi is based on a truly object-oriented framework structure, and changes made to the base class will be passed to all derived classes. The key technology used is VFI (Visual Form Inheritance), which can visualize form inheritance. VFI technology enables developers to dynamically inherit any other form in the current project or object library, and once the base form changes, the derived form is updated immediately.
Form inheritance has become a built-in feature as early as Delphi 5. To create a new form based on an existing form, Delphi will open the New Items dialog box by using the File | New menu command. This dialog lists all objects in the object library. Turn to the Forms page, which lists all forms that have been added to the object library; you can also select the PRoject page to select a form that is already in this project.
There are three options for adding a form to the project: Copy, Inherit, and Use. If Copy is selected, it means adding a copy of the selected form to the current project. If the form in the object library changes, it will not affect the copy in the current project. If Inherit is selected, it means that a new form is derived from the selected form and added to the current project. If the form in the object library changes, the derived form will also change. If Use is selected, it means that the selected form is added directly to the current project, as if the form was created by the current project. Using forms in Inherit means using visual inheritance - VFI.
3 Application examples
The following is an example to illustrate the application of VFI. In a MIS, it is necessary to count tables in multiple databases. In order to obtain a better display effect, the dxDBGrid control is used as the main control for displaying data results, and it is necessary to remember the Information such as the position of the field, the width of the field column header, etc., so a form is provided for each statistical interface to implement it.
To achieve this requirement, it is common to set each statistical form as follows: (For the convenience of explanation, the names of each component below use the default names)
1. Create a new form (providing a form class for each statistical interface);
2. Place the required components (place components such as dxDBGrid, wwDBNavigator, OpenDialog, etc.);
3. Set component properties (adjust the visibility, display width, etc. of each field); these tasks can be subdivided into two categories:
3.1: The same settings for all components in the form, such as the DataSet property of DataSource1, the DataSource properties of dxDBGrid1 and wwDBNavigator1, and wwFilterDialog1, etc.;
3.2: Set different tasks in each form. A typical operation is to set the data source of DataSet1 and the Column property in dxDBGrid1.
4. Set the event properties of forms and other components (such as opening the dataset in the form open event, closing the dataset in the form close event, and completing the export of dxDBGrid data in the export event).
The main components in the form are shown in the figure below:
[When I published it, I found that it would be fine without this picture, so I omitted]
In this process, some jobs have different requirements, such as work 1 and work 3; but some jobs are repetitive, work 2 and work 4. In this case, multiple statistical forms will appear between them. The cutting and copying of many codes are prone to errors, and when the program needs to be modified in the future, it is not easy to make complete and thorough modifications. For example, the code that implements the export of data in dxDBGrid1 may appear in multiple forms. Although encapsulating the exported data function into functions can be done to a certain extent, methods called in various statistical forms can reduce such repetitive code. However, for the settings of component properties and the judgment of most events, such as the determination of whether to "export selected data" availability in the PopupMenu pop-up event, it must appear in each form. (Although, a common OnPopup event processing can be set in the program, it is necessary to ensure that the menu item "Export Selected Data" has the same name, or if judgments are made in the program, duplicate work is still indispensable and errors are prone to occur. Or forget to set the properties of the menu item).
If VFI is used, these repetitive tasks can be solved. You can set a parent form (class name is TFormBaseTongji) for these statistics, the form looks like this:
[When I published it, I found that it would be fine without this picture, so I omitted]
All repetitive tasks, such as work 2, work 3.1, and work 4, are completed in the parent form. For example, set the code in OnFormShow: AdoDataSet1.Open;.
After that, each specific subform inherits from the form, and the settings performed in the subform only require work 3.2. The operations performed in this way will greatly reduce the workload. Not only that, using VFI can also maintain consistency between each form to have a similar appearance, thereby ensuring consistency of the software interface. Due to the elimination of repeated work, it also brings great benefits to software maintenance. For example, if you want to add another public function or adjust the details of the interface, it can be implemented in the parent form class, and the child form will no longer be used in the child form. Do any work; at the same time, VFI also allows child forms to increase their own functions and change the behavior in the parent form (i.e., implement polymorphism). It can be said that VFI provides a strong support for visual development.
4 Conclusion
Today, when reusability is becoming increasingly important, VFI provides the possibility for visual object-oriented development. Effective use of VFI can greatly improve program reusability, improve program consistency, and reduce maintenance costs.