function pointers do not play well, do not call yourself a C language master (function pointers are the best tool for decoupling object relationships, and signal)

Source: Internet
Author: User

Remember the beginning of work, a master told me that longjmp and setjmp play is not familiar, do not call themselves C language master. At that time I was doubtful, in order to let oneself to master direction, still spent a little time to learn longjmp and setjmp usage. It was later understood that it was not simply jumping and jumping, but rather a high-level exception-handling mechanism, which was indeed useful in some cases.


In fact, longjmp and setjmp are not familiar with the C language master, not causal relationship. However, if you can apply the master, I would like to say that if the function pointer play is not ripe, do not call yourself C language master. Why is it that the function pointers are so complicated? Of course not, anyone with a little bit of programming knowledge, whether he understands C or not, in 10 minutes, I think he'll be able to understand what a function pointer in C is going to be.

The reason is that the difficulty is not the concept of the function pointer and the syntax itself, but when and where to use it. The function pointer is not only a grammatical problem, but more importantly it is a design category. The real master of course should not only understand the grammatical level of skills, but also should understand the design method. Don't understand design, can calculate master? Doubt I'm exaggerating? Let's take a look at the design methods of the function pointers:

Related to layered design. Layered design has long been no new concept, the benefits of layering are well known, and the obvious benefit is simplifying complexity and isolating changes. Layered design, each layer is only concerned about their own things, which reduces the complexity of the system, the interaction between layers is limited to a very narrow interface, as long as the interface is unchanged, a layer of changes will not affect the other layers, which isolates the change.

The general principle of layering is that the upper layer can directly invoke the underlying function, and the lower layer cannot directly invoke the upper function. This is a simple sentence, in reality, the lower layer often in turn calls the upper function. For example, when you copy a file, a copy file function is called at the interface layer. The interface layer is the upper level, the Copy file function is lower layer, the upper layer calls the lower layer, for granted. But if you want to update the progress bar while copying the file, the problem comes. On the one hand, only the copy file function knows the progress of the copy, but it cannot update the progress bar of the interface. On the other hand, the interface knows how to update the progress bar, but it does not know the progress of the copy. What to do? The common practice is that the interface sets a callback function to the copy file function, and the copy file function calls the callback function at the appropriate time to notify the interface to update the state.

is related to abstraction. Abstraction is one of the most important concepts in object-oriented, and it is also a powerful aspect of object-oriented power. Object-oriented is just a kind of idea, as we all know, in C language can realize object-oriented programming. This is not for the sake of fashion, but a practical way. If you doubt it, you can look at the source code such as GTK +, Linux kernel.

Interfaces are the highest level of abstraction. Inside the Linux kernel, the concept of the interface is ubiquitous, like a virtual file system (VFS), which defines a filesystem interface, so long as you follow the specifications of this interface, you can develop a file system to hang up. Device drivers are more so, and different device drivers have their own set of different interface specifications. When developing a driver for your own development device, just follow the appropriate interface specification. How is the interface represented in the C language? It's simple, it's a set of function pointers.

is related to the interface and implementation separation. This is the first design guideline for the design pattern, which is designed for interface programming, not for implementation programming. The goal of separating the interfaces from the implementation is to isolate the changes. Software is a change, if not to isolate the change of things, leading to reaching, the cost is huge. This is not what everyone wants to see.

Since the C language can implement object-oriented programming, it is natural to use design patterns to separate interfaces and implementations. Like bridging mode, policy mode, state mode, proxy mode, and so on, there is no need to use function pointers in C language.

is related to the loose coupling principle. One of the reasons why process-oriented versus object-oriented is that it is not as object-oriented as it can visually map realistic models to computers. Process-oriented is a layer of control, and object-oriented more emphasis on the division of labor between objects. In the real world, the object is in the hierarchy relationship, and it is in the equivalence relation. In other words, the interaction between objects is often bidirectional. This strengthens the coupling between the objects.

Coupling itself is not wrong, in fact, coupling is essential, there is no collaboration without coupling, the object can not form a whole, what can not be done. The key is that the coupling needs to be appropriate, and the coupling should be as loose as possible in order to achieve the predetermined function. In this way, part of the system's changes will have little impact on other parts.

function pointers are the best tool for decoupling object relationships. Signal (such as the Signal in boost Signal and glib) is a typical example where the state of an object itself may be changing (or triggering some events), while other objects are concerned about its change. Once the object has changed, the other objects will perform the appropriate action.

If the object goes directly to a function that calls other objects, the function is complete, but the coupling between the objects is too tight. How to minimize this coupling, signal mechanism is a good way. Its principle is as follows: Other objects that are concerned about the object's change actively register a callback function in the object. Once the object has changed, call these callback functions to notify other objects. Functions are also implemented, but the coupling between them is reduced.

In C language, it is very difficult to solve these problems without using function pointers. In programming, if you've never thought of using a function pointer, it's hard to imagine you're a C-language whiz.

http://blog.csdn.net/adcxf/article/details/3975117

function pointers do not play well, do not call yourself a C language master (function pointers are the best tool for decoupling object relationships, and signal)

Related Article

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.