回调函数

作者: echoworlding | 来源:发表于2017-01-10 17:13 被阅读124次

回调函数是什么

回调函数,简称回调(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(application programming interface,应用编程接口),以供应用程序员使用;
  • 应用编程:利用写好的各种库来编写具某种功用的程序,也就是应用

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

图片来源:维基百科

一般,库对外提供的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;
}

相关文章

  • JavaScript函数_08回调函数

    回调函数 回调函数(回调),当我们把某个函数作为参数传递给另一个函数的时候,这个函数就是回调函数 回调函数的基本写...

  • Promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数。 回调地狱 回调套回调套回调套回调套回调套回调套回调....

  • 回调函数与promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数 具名回调写法 匿名回调写法 多层嵌套的匿名回调(回调地...

  • 回调函数与promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数 具名回调写法 匿名回调写法 多层嵌套的匿名回调(回调地...

  • javascript回调函数

    javascript回调函数很玄幻。 jquery 中大量使用了回调函数。直到现在才看懂 普通回调函数 匿名回调函...

  • 异步的实现

    异步的三种实现方式: 回调函数事件Promise 回调函数 回调函数不一定是异步 但是异步一定是回调函数。 事件 ...

  • JavaScript系列之回调函数callback

    JavaScript系列之回调函数callback JavaScript回调函数的使用是很常见的,引用官方回调函数...

  • mqtt python包回调分析

    mqtt的python包,回调函数比较复杂,每次在连接之前,需要先实现回调函数,回调函数的传入参数固定 将回调函数...

  • Android使用suspendCancellableCorou

    普通的回调函数: 回调方法,模拟耗时操作 去掉回调,转换为挂起函数:

  • Promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数一个最基本的具名回调匿名回调 回调地狱匿名回调嵌套过多层...

网友评论

    本文标题:回调函数

    本文链接:https://www.haomeiwen.com/subject/eiromttx.html