编程语言爱好者

回调函数

2017-01-10  本文已影响124人  echoworlding

回调函数是什么

回调函数,简称回调(Callback),是指通过函数参数传递到其它代码的某一块可执行代码的引用。
--wikipedia

通俗来讲,就是 如果一个函数f1里的参数 是另一个函数f2,则函数f1就称之为回调函数。

一个简单的实例:callback_demo.py

#!/usr/bin/python

def bark():
    print("Wang wang wang...")

def miaow():
    print("miao miao miao...")

def animal_sound(animal_name, sound_type):
    print("{} sound :".format(animal_name))
    sound_type()

if __name__ == "__main__":
    animal_sound("dog", bark)
    animal_sound("cat", miaow)

测试输出:

dog sound :
Wang wang wang...
cat sound :
miao miao miao...
[Finished in 0.0s]

在本例中,函数animal_sound即是回调函数。在使用函数animal_sound时,用户根据需要来传入叫声种类的函数名作为参数animal_sound的参数。

为什么要使用回调函数

编程分为两类:

所以,在抽象层的图示里,库位于应用的底下,而回调与应用处于同一抽象层,如下图所示:

图片来源:维基百科

一般,库对外提供的API需要尽可能保持稳定(想想如果Linux提供的系统调用或者Java/Python提供的包/模块的接口经常变动,你会是什么感受(×_×))。

But,作为库的开发人员,如果想扩充下库接口的功能,或者发现了某个功能有另一种更好的实现方式,该怎么办呢?没错,使用回调函数即可实现:改变回调函数的实现方式,也就改变了调用者的实现方式,而调用者的接口没有改变。

熟悉设计模式的同学也可以从设计模式中"接口与实现分离"原则和"策略模式"的角度来理解,调用者即接口,回调函数即实现

策略模式类图

怎么使用回调函数

现在看回调函数在一个具体程序中的使用:

程序功能:递归遍历目录,即递归列出目录中的文件列表。
demo code:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

#define MAX_PATH 1024

/* dirwalk: apply fcn to all files in dir */
void dirwalk(char *dir, void (*fcn)(char *)) 
{
    char name[MAX_PATH];
    struct dirent *dp;
    DIR *dfd;

    if ((dfd = opendir(dir)) == NULL) {
        fprintf(stderr, "dirwalk: can't open %s\n", dir);
        return;
    }

    while ((dp = readdir(dfd)) != NULL) {
        if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
            continue; /* skip self and parent */

        if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name))
            fprintf(stderr, "dirwalk: name %s %s too long\n", dir, dp->d_name);
        else {
            sprintf(name, "%s/%s", dir, dp->d_name); 
            (*fcn)(name);
        }
    }

    closedir(dfd);
}

/* fsize: print the size and name of file "name" */
void fsize(char *name) 
{
    struct stat stbuf;

    if (stat(name, &stbuf) == -1) {
        fprintf(stderr, "fsize: can't access %s\n", name);
        return;
    } 
    if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
        dirwalk(name, fsize); /* 回调fsize函数 */
    printf("%8ld %s\n", stbuf.st_size, name);
}

int main(int argc, char **argv) 
{
    if (argc == 1) /* default: current directory */
        fsize(".");
    else 
        while (--argc > 0)
            fsize(*++argv);

    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读