Flexible use of object-oriented ideas to efficiently program in BCB5
Note: This article is written in 2001 and is initially related to OO. Object-oriented development methods have become the mainstream of software development, and their technology has become increasingly mature, but many programmers are still accustomed to programming methods, such as a development environment like VC, easy to make C The programmer will focus mainly on the development of the development of software function code, but ignore the design idea for C -oriented objects. Borland's C Builder5 itself is an example of a complex software system for object-oriented technology. In the BCB5 IDE, an example of object-oriented technology applications can be seen everywhere. At present, object-oriented design is generally mostly used in the project design phase. When writing specific code, many will not take into account how to apply object-oriented ideas, but only pay attention to how to implement the function, how to make aesthetics, I think this is a regrettory. In fact, if you can truly guide the idea of object-oriented technology, you can greatly increase the development efficiency. Below I will introduce how to use new object-oriented ideas to guide programmakers in BCB5. First, use UML to describe the procedures you have to do:
UML (Unified Modeling Language) is a modeling language developed by the OMG organization. It has now become an industry standard. For programmers engaged in specific encoding, UML is used to describe the modules you designed, not only The development method of specification, easy to exchange cooperation with others, and easy your own understanding of the responsible development part, and make you take the first step to the object-oriented development method - to target your program Decompose into a class associated with each other. At present, the popular UML tool software is the most famous of Rational's Rose2000. After it is modeled, it can generate the C frame code directly after modeling. In fact, use a pencil and a white paper to use UML to perform system design, as long as it is its standard icon and identifier. I use the Case tool for Visual UML 2.5, and the sample pictures in the article are also taken from them. UML is quite flexible. For programmers, the most used is its class diagram, as shown below:
Figure one. The UML representation class diagram consists of three parts, the first part is the class name (information processing) of the class name (information processing), the second part is a class of data; the third part is a list of functions of the class . The information of the class can be explained by adding various annotations or documents to the class diagram. By encapsulating the features in your program into one class, you can know all the members of the class from the figure, without opening. H header files, do not need to rely on the automatic list function provided by the development environment. The most convenient place in UML is that it specificallys the association relationship between a set of classes, so that you can easily draw the relationship between modules and classes of the entire system, expressed the connection between classes with UML, You can easily know if you want to modify some categories, which related classes may affect, thus making a good foundation for your debugging procedure, capture program errors. Figure 2 and Module Call Figure 2 shows the call diagram of the form class. From the figure, you can clearly see the call relationship between the form and the content of information transmission. By adding different colors to classes, you can divide the class zone into different types: the class and call others called only by others. the type. If you do not automatically generate a code, you can also add a list of relevant members of the class (not necessarily all of the most important) in the figure, which is more important in the picture. Comprehensive. For a programmer, the call diagram of drawing classes and modules is critical and useful. By carefully analyzing this picture, the programmer can do a lot of work: 1. It will be found that some modules are repeatedly called, so if they improve these modules, enhance its robustness and execution efficiency will have a good impact on the program itself; 2. It will find that the functions between different classes have the same place. At this time, the functionality can be extracted, becoming an abstract class, the original class is changed to inheritance, which is turned to the object-oriented development method Development method; 3. There will be some operations between the entire program (such as file operation, the operation function of a control) is used multiple times by many modules, you can extract all of these public operations, and then classify a number of classes, so public operations Transformation into a class of function members, when you need to use a certain function, just a corresponding object, then call its member function, immediately remove, no longer need to disperse the function code between different modules; 4. It can be arranged in the development order. Calling by others, the classes and modules that do not call others must first complete, the modules that implement the main functions must be developed in modules that implement the secondary function; 5. When the expansion function is needed, it is easy to locate how the program should be modified, and the work plan is to be expanded: Is it an existing class or a new class, and the new class is to be inserted, what module may be Effective. In addition to the class, UML also provides a statistical map, interaction, cooperation, and examples of examples such as examples. No matter what form is adopted, as long as people can easily grasp the entire software system, it is more beneficial to improve software. Development efficiency. Therefore, the software community puts the UML as a major advancement developed by the object-oriented field, I agree. Second, the design problem of user interface - form and components are also a class
The design interface may be one of the most sense of job, especially the programmer who uses VB and the rapid development environment (RAD), developing a program's first step may be the first interface, then Various event write code for the control of the button. This most natural and intuitive development method is in line with people's thinking habits, I think this method is not bad, after all, a form is much better than a class of abstraction in the paper. But we also need to do this from an object-oriented perspective. After making it clear about your program, you can design a form frame first, decide which form will pop up after a button, that is, the call relationship between the form, this, can quickly take the BCB quickly This type of fast interface design tool has developed a software model that can be run, you can give users (of course, if you are using it, you don't have to do this) demonstration, then make corresponding adjustments, do you can avoid the project's future interface Changes, at the same time, in the process of designing the entire interface framework, there is a further understanding of the problem to be solved by the software; after the form frame is designed, it can consider the non-visual partial design, which can be divided into two Part: First, the module group of the software actual handling function is completed. Use the term software engineering to "business logic layer", this layer is independent of the user interface; the other part is the user interface function to implement the module group, Mainly used to perform various processing on the user's input, closely related to the interface; in general, the design of the business logic layer class may begin in the system analysis phase, and may have completed when designing the user interface. However, for smaller projects, or software developed by programmers, the design of these two parts is often done simultaneously after the design interface is completed. The user interface design is completed, don't have a compilation code, you should use the UML to draw the form module call map (see 2 above), the members of the form can be empty, the key is to draw all the forms Call the relationship while clarifying what functionality to implement each form. For this picture, you can also do a lot of work, please keep in mind: "Forms and controls are also a class"! 1. First, you have to figure out what parameters passing between the forms, add it to the formal line between the form. 2. If the function between the two forms is basically the same, there is only a small amount of difference, such as a few buttons, then the common portion can be extracted, as a form base class, in this base class form Writing the basic function code, then put it add to repository, then in the object of New a base form, you can directly modify its interface, overload its event; please use UML Referring to Figure 3, there is a variety of members in the form; If the two form interfaces can be designed, only the data information that only processes, the multi-state constructor (two or more configuration functions, but the parameter is different), two forms will be used. One. Figure three inherited form
The BCB5 uses a VCL component that can be used to use them more effectively: 1. Inheriting existing components, generating its own VCL components, thereby expanding or customizing its function, meeting the specific requirements of the program; 2. With the component aggregation function provided by BCB5, it is realized for component reuse. There are two main methods: Component Template and Component Framework (TFRAME), which makes it easy to manual (can refer to BCB5 random manual); "Code reuse" Improve the key to software development efficiency, through the inheritance of the form and the reuse of the VCL assembly, can greatly improve the cohesiveness of the software, reduce the coupling degree between the modules. Third, hierarchical problem
Each software has its most suitable software architecture, which is determined according to the situation of specific items. At present, the hierarchical architecture is used in this architecture, and each layer is composed of several groups, and the services between the layers can be unidirectional or bidirectional. When specifically developed, the associated object (class) is combined in one module, and each layer may have multiple modules. With the UML method, you can use the package (package) to represent this hierarchy of the software, and Define dependencies between modules (package). The overall hierarchy of Figure 4 is a sliding structure of a software. I would like to use BCB5 as an example. For programmers for specific writing code, introduce the layering of form code, this can have two implementations : 1. Using the ActionList component management, all function functions are added to this component; then, you can specify its function name to user interface controls such as buttons and menus (even the icon can pass in one time, this function of BCB is very convenient Practical); 2. Write the functionality to be implemented as a private (or public, or protected) member function of TForm classes; these two methods are actually the same, the key is to write in direct component event functions (such as button Click event) that receives user input. Processing code, but writes the name of the function called, the advantage is to reduce the dependence of the code and the interface. For example, if you suddenly want to change the icon TButton to have icon TbitBtn, it is basically no need to change, To a certain extent, the variability of the form interface is enhanced, making modifications to the form (especially have written a lot of code). This method can also be employed for the form, which can be used for unacceptable classes. Fourth, the information transmission problem between modules:
When using the VC to design, the programmer is going to deal with the message issued with Windows. This allows the programmer to make full use of the functionality of the operating system, develop a powerful program, but due to its cumbersome, request procedures The staff has a good understanding of Windows workflow and principles, and the amount of code is not high, which is also a reason for the length of project cycle using VC development. BCB5 uses VB development ideas, encapsulating a variety of Windows messages with object-oriented ideas, so that the programmer may not deal with messages such as WM_Click (unlike VB, BCB5 can also easily process Windows. Messages, or define their own messages), and concentrate on how to solve the information transfer issues between modules with object-oriented ideas. Summarize, the information can be delivered in the following ways (class): 1. Setting the conformation in the constructor of the class, so that the information can be carried by the instance of the class, which often has a plurality of constructors (such as a form class described above); 2. In the class, design properties, after generating this object, the caller is set to it, thereby transmitting the corresponding control information to it, and the caller can also acquire the processing after processing; 3. Set up a static member in the class to share information in all instances of the class; Generate global objects (in BCB5, you can create global objects in the "Engineen Name .bpr" file), information on this object is shared throughout the program; 5. Information can be transmitted by generating an external file or setting a dedicated database field; of course, there are other ways to transmit information, but in the object-oriented programming, try to avoid direct use of Windows message mechanism to deliver information unless it is special Need. to sum up: