C語言中回調(diào)函數(shù)的含義與使用場景詳解(2)
詳解C語言中回調(diào)函數(shù)的含義與使用場景(2)
引言:在上一篇文章中介紹了回調(diào)函數(shù)的概念與使用方法,本節(jié)將深入地介紹回調(diào)函數(shù)典型的使用場景。通過使用回調(diào)函數(shù)可以實(shí)現(xiàn)驅(qū)動(dòng)和應(yīng)用程序的分離解耦,讓程序更加地靈活。也可以借助回調(diào)函數(shù)實(shí)現(xiàn)插入自定義代碼、分層設(shè)計(jì)程序的思想。
使用場景一(重定義):
在統(tǒng)一的接口中,動(dòng)態(tài)地改變一個(gè)函數(shù)的功能。該函數(shù)的功能可以是加載參數(shù)、或者執(zhí)行運(yùn)算。示例如下:
typedef int (*my_calculate_t)(int a, int b);
static int cal_sum(int a, int b)
{
printf("now is sum\r\n");
return a + b;
}
static int cal_sub(int a, int b)
{
printf("now is sub\r\n");
return a - b;
}
static int cal_mul(int a, int b)
{
printf("now is mul\r\n");
return a * b;
}
static my_calculate_t s_cal = cal_sum;
static int test2_cal (int a, int b)
{
int result = 0;
if(s_cal) {
result = s_cal(a ,b);
printf("result=%d\r\n", result);
}
return result;
}
void app_main(void)
{
printf("init done\r\n");
int m = 10, n = 1, ret;
ret = test2_cal(m, n);
}
上述代碼通過 test2_cal() 實(shí)現(xiàn)計(jì)算接口的統(tǒng)一。只需改變函數(shù)指針 s_cal 的值,就可以讓 test2_cal()執(zhí)行不同的功能。我們可以拷貝上述程序分別對(duì) s_cal賦值 cal_sum、cal_sub、cal_mul實(shí)現(xiàn)在不改動(dòng)其他代碼的情況下,讓 test2_cal 執(zhí)行不同的運(yùn)算。這種通過改變函數(shù)指針 s_cal 的值,讓函數(shù) test2_cal() 執(zhí)行不同功能的特性,可以稱之為重定義了test2_cal()的功能。
上述程序運(yùn)行結(jié)果:
init done
now is sum
result=11
使用場景二(擴(kuò)展函數(shù)功能):
可以在程序中定義多個(gè)回調(diào)函數(shù),若定義了就執(zhí)行,否則就略過。實(shí)現(xiàn)在函數(shù)中擴(kuò)展更多代碼的目的(就像一個(gè)鉤子函數(shù)一樣)。示例如下:
typedef int (*my_calculate_t)(int a, int b);
static int cal_sum(int a, int b)
{
printf("now is sum\r\n");
return a + b;
}
static int cal_sub(int a, int b)
{
printf("now is sub\r\n");
return a - b;
}
static int cal_mul(int a, int b)
{
printf("now is mul\r\n");
return a * b;
}
static my_calculate_t s_c_array[5] = {cal_sum, cal_sub};
static int test1_cal(int a, int b)
{
volatile int result = 0;
volatile size_t i = 0;
for(i=0; i<(sizeof(s_c_array)/sizeof(my_calculate_t)); i++) {
if (s_c_array[i] != NULL){
result = s_c_array[i](a, b);
printf("i=%d, result=%d\r\n",i, result);
}
}
return result;
}
static void my_cal_calculate_register(my_calculate_t cal)
{
for(size_t i=0; i<(sizeof(s_c_array)/sizeof(my_calculate_t)); i++) {
if (s_c_array[i] == NULL){
s_c_array[i] = cal;
return;
}
}
}
static void my_cal_calculate_unregister(my_calculate_t cal)
{
for(size_t i=0; i<(sizeof(s_c_array)/sizeof(my_calculate_t)); i++) {
if (s_c_array[i] == cal){
s_c_array[i] = NULL;
return;
}
}
}
void app_main(void)
{
printf("init done\r\n");
int m = 10, n = 2, ret;
printf("test 1***************begin\r\n");
test1_cal(m, n);
printf("test 1***************end\r\n");
printf("test 2***************begin\r\n");
my_cal_calculate_register(cal_mul);
test1_cal(m, n);
printf("test 2***************end\r\n");
printf("test 3***************begin\r\n");
my_cal_calculate_unregister(cal_mul);
test1_cal(m, n);
printf("test 3***************begin\r\n");
}
上述代碼通過在函數(shù) test1_cal()增加一個(gè)指針數(shù)組 s_c_array[] 來控制函數(shù) test1_cal()中實(shí)際執(zhí)行調(diào)用哪些函數(shù)。默認(rèn)的情況下,它僅調(diào)用 cal_sum 和 cal_sub兩個(gè)函數(shù),通過函數(shù) my_cal_calculate_register()可以增加它調(diào)用的函數(shù),示例中 my_cal_calculate_register(cal_mul);語句增加了其內(nèi)部調(diào)用一個(gè) cal_mul函數(shù)。
運(yùn)行結(jié)果:
init done
test 1***************begin
now is sum
i=0, result=12
now is sub
i=1, result=8
test 1***************end
test 2***************begin
now is sum
i=0, result=12
now is sub
i=1, result=8
now is mul
i=2, result=20
test 2***************end
test 3***************begin
now is sum
i=0, result=12
now is sub
i=1, result=8
test 3***************begin
使用場景三(分層):
通過在結(jié)構(gòu)體中使用 函數(shù)指針來實(shí)現(xiàn)程序的分層設(shè)計(jì)。分層帶來的好處是方便維護(hù)與結(jié)構(gòu)清晰。
typedef int (*my_calculate_t)(int a, int b);
typedef int (*add_self_t)(int a);
typedef void (*send_to_printf_t)(int a);
typedef struct my_test_struct_t
{
my_calculate_t m_calculate;
add_self_t m_add;
send_to_printf_t m_printf;
}my_test_struct_t;
static int cal_sub(int a, int b)
{
printf("now is sum\r\n");
return a - b;
}
static int cal_add_self(int a)
{
return a+1;
}
static void cal_send_to_printf(int a)
{
printf("total is %d\r\n", a);
}
static void cal_send_to_printf2(int a)
{
printf("now total is %d\r\n", a);
}
my_test_struct_t s_test = {
.m_calculate = cal_sub,
.m_add = cal_add_self,
.m_printf = cal_send_to_printf,
};
static int test1_cal(int a, int b)
{
int result = 0;
if(s_test.m_calculate){
result = s_test.m_calculate(a,b);
printf("result1 is %d\r\n", result);
}
if(s_test.m_add){
result = s_test.m_add(result);
printf("result1 is %d\r\n", result);
}
if(s_test.m_printf) {
s_test.m_printf(result);
}
return result;
}
void app_main(void)
{
printf("init done\r\n");
int m = 10, n = 2;
printf("test 1***************begin\r\n");
test1_cal(m, n);
printf("test 1***************end\r\n");
printf("test 2***************begin\r\n");
s_test.m_printf = cal_send_to_printf2;
test1_cal(m, n);
printf("test 2***************end\r\n");
}
上述程序中通過在結(jié)構(gòu)體 s_test中使用三個(gè)函數(shù)指針 m_calculate、m_add、m_printf來實(shí)現(xiàn)三個(gè)步驟:計(jì)算、自增、打印,三層功能的分層。每個(gè)層都是一個(gè)函數(shù)指針,所以每一層都可以通過改變函數(shù)指針的值,實(shí)現(xiàn)重新定義。
運(yùn)行結(jié)果:
init done
test 1***************begin
now is sum
result1 is 8
result1 is 9
total is 9
test 1***************end
test 2***************begin
now is sum
result1 is 8
result1 is 9
now total is 9
test 2***************end
總結(jié)
本篇內(nèi)容作為上一篇文章的深化,重點(diǎn)講述了回調(diào)函數(shù)的三種典型使用場景:
- 實(shí)現(xiàn)函數(shù)功能重定義
- 擴(kuò)展函數(shù)功能
- 實(shí)現(xiàn)程序分層設(shè)計(jì)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++中的std::initializer_list使用解讀
這篇文章主要介紹了C++中的std::initializer_list使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
C語言鏈表實(shí)現(xiàn)通訊錄系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語言鏈表實(shí)現(xiàn)通訊錄系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C++?自增自減運(yùn)算符的實(shí)現(xiàn)示例
本文主要介紹了C++?自增自減運(yùn)算符的實(shí)現(xiàn)示例,自增和自減運(yùn)算符在C++中主要用于循環(huán)語句中,使循環(huán)變量的值自動(dòng)+1或者-1,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用詳解
本篇文章是對(duì)c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

