OO IN C (4): Object-oriented thinking in C language

xiaoxiao2021-03-06  55

Object-oriented thinking in C language

I often hear someone else's object-oriented programming. When I was in class, I also opened the class for object-oriented program. But unfortunately, these are based on C , even VC . More unfortunately, I have always been a C users for many years. At school, I mainly do is the drive layer on the hardware, and the underlying functional layer. After work, it is done the software development on your mobile phone, all of which are in which you are in which you are. Although I have to say that C is a very good language, but its compilation speed, code efficiency, compiled code size limit it in embedded applications. (However, the current embedded CPU is getting faster and faster, the memory capacity is bigger. I don't think there should be no problem with C . This makes me feel like an embedded compiler. Although Philip and Ti seem to have C compilation The device, but it seems that no one uses this. Is it too expensive?

So can the C language generated in the process-oriented era uses object-oriented ideas? I think it is definitely, C has joined the support of the object on the language level while providing a rich object library. And under the C language, we have to be self-reliant.

I. The purpose of object-oriented thinking is to frameize, the means is abstract

I believe that many people understand what is going on: class, abstract class, inheritance, and polymorphism. But what is the cause of these concepts?

Bi-side saying: You go to buy a monitor, but the brand style of the display is diverse, and what you happen in the process of buying is also unpredictable. For such things, how do we describe in the program language. Object-oriented thinking is to solve such problems. Write a program (even a project), which is difficult to use, from richness is more difficult. Object-oriented uses individual behaviors of the program to objects, and uses abstract methods to classify (abstraction), thereby simplifying intricate things into several major organic combinations (framework).

In fact, there are many things around us, such as computers: Computers are composed of a motherboard, CPU plus various cards. This is a framework. Ignore different CPUs, different motherboards, different sound cards, network cards, graphics card differences, this is abstract. For example, now the current education network: is a major core node: Tsinghua, Peking University, Bei Post, etc., and then all child nodes, constitute a whole education network network.

So I think that object-oriented programming is: a large engineering is a hierarchical structure, and each layer is connected to an overall (framework), and each abstract structure is independent of each other and can evolve independently (inherit, Polymorphism). The hierarchy, the structure has a unified communication method (usually a message, event mechanism).

Second, the "object-oriented" method commonly used in previous C language programming

In fact, since the birth of C language, people have thought of many ways to reflect the "object-oriented" ideas. Let's talk about the method I know. Let me say some people familiar with Dongdong, and slowly talk about it. Ha ha

1. Macro definition: Some people can't help but ask, how to pull it here, we can first look at a simple example: #define macrofunction Afunction and then call a lot of AFUNCTION in the program, but one day, you suddenly discovered You have to use BFunction, (but Afunction can't, it is very likely that you will call later), this time you can #define macrofunction bFunction to achieve this. Of course, I have to say such a way is to simple, sometime naïve, because a very funny question is if I generally change to bforction, what should I do if I don't change? Then I have to find it back. 2. Static entry function, guarantee the same name, using flag bit call subunies: This typical application is a lot, such as the network card driven has an inlet function Nilan (int functioncode, para *). The specific parameters are not clear. However, Nilan's main body is like this: Long Nilan (int functioncode, para *) {

Switch (functioncode) {

Case SendPacket: Send (....)

Case receivePacket: Receive (...)

... ..

}

Write here, you understand what it means. To ensure the same function name: NIC driver is the PNA protocol stack, so how to ensure that PNA protocol stacks and different drivers are compatible, a simple way is to use only one portfolio. Call the internal functions by changing the parameter value of the function. This approach is possible: If you want to call a new function later, it will increase the corresponding function parameter value. If we see the NIC driver and PNA protocol stack as two layers, we can find:

The interconnect port between layers and layers is small (here an inlet function), which is generally used by name parsing rather than a specific function call (using the functionCode call function, Nilan only implements the function of the name resolution) - ! Interface restrictions and name analysis

Interface restriction: only a limited function between layers and layers

Name parsing: Establish a common name and function between the layer and the layer, and use the name call function.

3. Callback function. I think this is a creative of C language. Although it is very simple, it is like how to put the eggs, but if you didn't expect, hehe. If the static entry function implements a manageable macro, Callback is a cargo-to-regulatory micro: it makes a function to add functions without recombining! But in the earliest time, there are also many people holding an objection because it uses a function pointer. Although the function pointer is flexible, because it is necessary to access the memory twice to call the function, the first access function pointer, the second is true function call. Its efficiency is not as good as a normal function. But in a less demanding environment, the function call itself is not very much, the performance of the function pointer is not particularly bad, and the function pointer is actually a best choice. However, in addition to performance, the most troublesome place will lead to "bracketing" of the program. Imagine: In the program, when you read a function pointer, if you don't know which function is pointing to this function, the feeling is really bad. (You can look at the article, you should use the advanced program framework to avoid this situation). Third, Event and Message have seen the above description, I believe how many things you have to use Event and Message. Specific function calls bring a lot of problems (although it is efficiently, this is good). In order to improve the flexibility of the program, Event and Message are generated. The method parsed by name replaces the usual function call so that if the two parties are consistent with such resolution, it can reach a uniform. However, the role of Event and Message is not just the case. Event and Message also have functions of communication between processes. The process sent its messages to "Control Center" (simple is a message queue, and the content of the WHILE loop will continue to resolve the queue, the control program gets the message, distributes to the corresponding process, so other processes can Get this message and respond. Event and Message are flexible because you can add or close a process at any time, (just a list of purchasing distribution messages) Event and Message will feel the same, just the concept. Event is mostly used to refer to an action, such as what happens in hardware, what functions need to call a function. Message is mostly used to refer to an indication, such as what is the operation command, and more. Fourth, small knot

In fact, the programs are the same as writing articles, they have an outline first, then slowly enriched. First abstract to get the skeleton of the program, then consider other contents of all aspects: What happens when the program is extremely? The function of this place is still not perfect, and will there be any problem with the future? Is the program scalable? This series of articles is some of my experiences in these phases. The purpose is to throw bricks, hoping to communicate with you, get more knowledge. Liyuming1978@163.com (this mailbox has previously issued a post of COwen, now it is fast into the garbage mailbox, huh, huh, the power of the network is really powerful)

转载请注明原文地址:https://www.9cbs.com/read-109531.html

New Post(0)