Object-oriented C language thought

Transfer from: http: //hi.baidu.com/todaygoodhujun/blog/item/0bc9c4ce28e2dc0392457e31.html

 

Often I hear people say that the object-oriented programming, have previously been to this class of object-oriented programming. But unfortunately, these are based on C ++, and even VC ++ based. And even more unfortunately, over the years I have been a user of C. In school, I mainly do is drive on the hardware layer, and the underlying functional layer.

      After work, they do software development on the phone, and C all of which are inseparable. Although I have to say, C ++ is a great language, but its compilation speed, code efficiency, the compiled code size have limited its application in embedded. (Although now more and more embedded CPU speed, memory capacity becomes large, I think in C ++ should be no problem. It makes me feel it seems to be limiting the embedded compiler. Compiler although Philip and TI seems to have the C ++ It is, but no one seems to use this. is it too expensive? but anyway, embedded applications, commonly used C language is yes)

      Then the process-oriented era produced the C language can use object-oriented thinking it? I think it is certainly possible, C ++ but added support for object at the language level, while providing a rich object libraries. And in the C language, we had to self-reliance.

      First, the purpose of object-oriented thinking is framing, means an abstract

      I believe many people understand what object-oriented talk: class, abstract classes, inheritance, polymorphism. But what prompted these concepts to produce it?

      An analogy: you buy a monitor, but the monitor brand styles are diverse, what you place in the buying process is also unpredictable. For such a thing, we describe how to program in the language yet. Object-oriented thinking is such a problem to solve. Write a program (even that is a project), never to use is difficult from there to the rich is even more difficult. The behavior of each object into an object-oriented program, but in an abstract way to classify these objects (abstract), which will simplify complex things into several major organic combination of (framing).

      In fact, a lot of things around us are so constituted: For example: Computer is the motherboard, CPU cards plus a variety of compositions. This is a framework of. While ignoring different CPU, different motherboard, different sound card, graphics card difference, which is abstract. Another example is now Education Network: is the main core nodes: a few Tsinghua University, Peking University, Beijing University of Posts and Telecommunications, etc., then each child node, in turn make up the entire education network network.

      So I think the idea that object-oriented programming: a large-scale project is a hierarchical structure, and each layer is connected by the abstract structure as a whole (framing), between the various abstract structures are independent of each other, can be evolved independently (inheritance, polymorphism). Between levels, each with a unified means of communication between the structure (usually a news event mechanism).

      Second, before the C programming language commonly used in "object-oriented" approach

      In fact, since the C language was born, people wanted a lot of ways to reflect the "object-oriented" thinking. Here is the method I know to say.

      1. macro definitions:

      Some people ask, macro defines how side-tracked here, and we can look at a simple example:


      #define MacroFunction Afunction


      Then in the program which you call a lot of AFunction, but one day, you suddenly find that you use the BFunction, (but AFunction can not do, is likely to have to call you later), this time, you can #define
      macrofunction Bfunction to achieve this purpose.

      2. Static entry function (abstract interface to achieve?) , To ensure the same function name, use the flag to call a subroutine:

      Typical of such applications are many, for example, which has a network card driver entry function Nilan (int
      FunctionCode, Para *). Specific parameters of what remember. But the main NiLan is this:


      Long Nilan (int FunctionCode, Para *)

      {

      Switch(FunctionCode)

      {

      Case SendPacket:

      send(….)

      Case ReceivePacket:

      receive(…)

      …..

        }


      I write to you to understand what we mean it. That will ensure the same function name: pNA + network card driver and protocol stack are interconnected, then how to ensure pNA + protocol stack and different drivers are compatible with it, a simple solution is to use only one entry function. If the parameter value by changing the function, each call to an internal function.


      This approach is evolutionary: If you later want to call the new function, increase the corresponding function parameter values ​​just fine. If we pNA + NIC driver and protocol stack as two layers, we can find: interconnect interface between the layers is very small (this is a function entry) is generally used instead of the name resolution approach specific function calls (use FunctionCode call functions, Nilan only achieve name resolution function) -! Interface restrictions and name resolution


      Interface limitations: limited only known function between layers

      Name resolution: establishing correspondence between the common name and function between layers, using the name of the calling function between.

      3. CALLBACK function (virtual interface implemented by the structure and function pointers)

      I think this is a C language of the initiative, although it is very simple, like how to stand up, like eggs, but if you did not expect it, remains a challenge. If the static entry functions to achieve macro, then a manageable, CallBack is to achieve a micro-evolution: it makes a function can add functionality without recompiling! But in most early days, but also find many people opposed, because it uses a function pointer.

      Although flexible function pointer, but because it wants access memory twice before they can call to the function, the first visit of function pointers, the second is the real function call. Its efficiency is not as good as ordinary functions. But in a less demanding environment, the function call itself is not how time-consuming, the performance of the function pointers are not particularly bad, in fact, use the function pointer is the best choice.

      But the function pointer in addition to performance, the most troublesome areas that will lead to "fragmented" program. Just think: In the program, you read a function pointer when stunned if you do not know the function pointer which function, that feeling really bad. (You can see behind the article, to the use of advanced procedural framework, to avoid such a situation)

      Three, Event and Message

      Read the above description, I believe we somewhat understand why you want to use the Message and Event. Specific function call will bring a lot of problems (although in terms of efficiency, this is very good). In order to increase the flexibility of the program, Event and Message approach produced. Instead of the usual function call with the name resolution approaches, so that if the two sides to resolve this is consistent, then we can achieve a uniform. However, Event and Message role was not just the case.

      Event and Message functions as well as the establishment of inter-process communication . The process their message to the "control center" (that is, a simple message queue, and continue to take a while loop content of the message queue and execution), the control program to get the message, distributed to the corresponding process, so that other processes can get the message and respond.

      Event and Message is very flexible, because you can always add or close a process, (only need to add the list to distribute messages on it) Event and Message from the program will achieve I think it is the same, but different concepts. Event and more used to refer to an action, such as hardware what happened, what you need to call a function, and so on. Message used to refer to more than one indication, such as what happened program operation command, and so on.

      IV Summary

      In fact, programmed and write articles, are the first to have an outline, and then slowly rich. First get abstraction framework program, and then consider other aspects of the content of each: for example, when the program will be extremely what happened? This place is now a function of the program is not perfect, perfect later what the problem? Program is not extensible?

Reproduced in: https: //my.oschina.net/dake/blog/196635

Guess you like

Origin blog.csdn.net/weixin_34146410/article/details/91508470