Function pointers and Software Design

Source: Internet
Author: User
Source: http://dev.csdn.net/author/absurd/6b43759193e74da4a4d911c0593af9f2.html

I remember when I first started my work, a master told me that longjmp and setjmp are not familiar with themselves and should not be called a C language master. At that time, I was skeptical that it took me a little time to learn the usage of longjmp and setjmp in order to move myself towards the master. Later, I realized that it was not just as simple as jumping in and out, but an advanced Exception Handling Mechanism, which is indeed useful in some cases.

 

I also used it several times in my program to show my skills. It is gradually discovered that the benefits brought by such a technique are at a cost, undermining the structural design of the program and making the program hard to read, especially for beginners. Finally, I understand that this kind of technique is just a seasoning. It can be used several times in a few cases to simplify troubleshooting. If you take the seasoning as a meal, the system will put it upside down, and the program will show malnutrition.

 

In fact, longjmp and setjmp are not familiar with the C language skills, not causal relationships. However, if you can use that expert, I would like to say that if the function pointer is not familiar with it, you should not use it as a C language master. Why? Is the function pointer so complicated? Of course not. Anyone with a little programming knowledge, whether he understands C language or not, can understand what the function pointer in C language is in 10 minutes.

 

The original reason is that it is not the concept and syntax of function pointers, but when and where to use it. Function pointers are not only syntactic issues, but more importantly, they are a design category. Real masters should not only understand grammar skills, but also design methods. I don't know about design. Can I be a master? Are you sure you want me to boast? Let's first look at the design methods related to the function pointer:

 

AndHierarchical Design.Layered design is not a new concept. The benefits of layered design are well known. The obvious advantage is that it simplifies complexity and isolates changes. With a layered design, each layer only needs to care about its own things, which reduces the complexity of the system. The interaction between the layer and the layer is limited to a very narrow interface, as long as the interface remains unchanged, changes in a layer do not affect other layers, which isolates the changes.

 

Generally, the upper layer can directly call lower-layer functions, while the lower layer cannot directly call upper-layer functions. This sentence is simple. In reality, the lower layer often calls upper-layer functions in turn. For example, when you copy an object, you call a copy object function at the interface layer. The interface layer is the upper layer, the copy function is the lower layer, and the upper layer calls the lower layer. However, if you want to update the progress bar when copying a file, the problem arises. On the one hand, only the copy object function can know the copy progress, but it cannot update the progress bar on the interface. On the other hand, the interface knows how to update the progress bar, but it does not know the copy progress. What should I do? A common practice is to set a callback function on the interface to notify the interface to update the status when appropriate.

 

AndAbstraction.Abstract is one of the most important concepts in object-oriented systems and also a powerful feature of object-oriented systems. Object-oriented is just an idea. As we all know, object-oriented programming can be implemented using the same C language. This is not just a fashion, but a practical method. If you are skeptical about this, you can check the Open Source Code such as GTK + and Linux kernel.

 

An interface is the highest level of abstraction. In Linux kernel, the interface concept is everywhere. For example, a virtual File System (VFS) defines an interface of a file system, as long as it complies with the specifications of this interface, you can develop a file system and mount it. This is especially true for device drivers. Different device drivers have their own set of interface specifications. When developing drivers on your own, you only need to follow the corresponding interface specifications. How do I express an interface in C? A set of function pointers.

 

AndSeparate interfaces from implementationsRelated. Interface Programming, rather than implementation programming, is the first design criterion in design patterns. The goal of separating interfaces from implementations is to isolate changes. Software is changing. If we cannot isolate the changed things, the cost will be huge. This is what we don't want to see.

 

Since the C language can implement object-oriented programming, You can naturally use the design pattern to separate interfaces and implementations. Such as the bridge mode, policy mode, status mode, and proxy mode. in C language, function pointers are required.

 

AndLoose coupling principleRelated. One of the reasons why process orientation is pale compared with object orientation is that it can intuitively map realistic models to computers, unlike object-oriented ones. Process-oriented refers to layer-by-layer control, while division of labor and cooperation between objects with greater emphasis on objects. In the real world, there are few objects in hierarchies and many objects in the peer relationship. That is to say, the interaction between objects is usually bidirectional. This will enhance the coupling between objects.

 

Coupling itself is correct. In fact, coupling is essential. Without coupling, there is no collaboration. Objects cannot form a whole, and nothing can be done. The key lies in the proper coupling. The coupling should be as loose as possible on the premise of implementing the predefined functions. In this way, some changes in the system have little impact on other parts.

 

Function pointers are the best tool for decoupling object relationships. The signal mechanism (such as boost signal and signal in Glib) is a typical example. The state of an object may change (or trigger some events ), other Objects care about its changes. Once the object changes, other objects must perform the corresponding operations.

 

If the object calls functions of other objects directly, the function is complete, but the coupling between objects is too tight. How can we minimize this coupling? The signal mechanism is a good solution. Its principle is roughly as follows: other objects that focus on the changes of this object actively register a callback function to this object. Once this object changes, 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 never thought of using function pointers, it is hard to imagine that you are a C language expert.

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.