分享帖:关于回调

风之子赛拉飞尔 2015-05-19 05:47:21
今天用到回调,也看了些网上的文章,这里分享一下个人的心得体会

回调,字面意思是返回的调用,是相对于“调用”而言的。A调用B,B的执行过程中又反过来调用A,这就是回调。
以下约定,其中A称为客户端,B称为服务端,以便于描述。

回调是一种设计思想,“回调就是对函数指针的一种运用方式”这只能说是语法层面的理解,
还没有达到设计层面的高度。设计模式中,访问者模式(Visitor Pattern)中的两个要素之间就存在回调关系。
也就是说通过服务端B封装一些逻辑过程,但其中某个环节又需要暴露给客户端A,以提供自由度。

回调的范式1:

{
typedef int (__stdcall *CallbackFunc)(LPARAM P1, LPARAM P2, LPARAM P3);
int ServerFunc(LPARAM P1, CallbackFunc fn, LPARAM P3);
}

ServerFunc是主调函数,是服务端B提供给客户端A的接口,由客户端A提出规格要求。
CallbackFunc是回调函数,是客户端A提供给服务端B的回调接口,由服务端B提出规格要求。
P1是A传给B的参数,P2是B传给A的参数,P3是A发起的需要传入回调函数的参数。

这里参数P3是一个奇怪的存在,因为P3对于B没有任何意义,但是却不得不在B的接口中体现,
设计上可以称之为“对B的接口打洞”。实际应用中还确实有这种需要。那么如何解决这个问题呢?

还记得标准库中的仿函数算子(Functor)么,譬如std::less,也是一种回调的实现方式,其原理是,
实现一个类(或结构体),重载圆括号运算符,即相当于回调函数。想想使用类有什么好处呢?
对了,类是可以有成员变量滴,哦耶,这下我们可以把参数P3做为Functor类的成员变量了,如果
需要增加P3类参数,也不再影响服务端B了,very ok。

回调的范式2:

{
// 服务端B
struct ICallbackFunctor
{
virtual int operator()(LPARAM P1, LPARAM P2)=0;
};

int ServerFunc(LPARAM P1, ICallbackFunctor fn);

// 客户端A
struct CallbackFunctor : public ICallbackFunctor
{
virtual int operator()(LPARAM P1, LPARAM P2)
{
}

LPARAM P3;
};
}


再说说泛型化,标准库是用模版来实现泛型的,其实使用多态也是可以的,别说想不到,
重载运算符也是可以声明为virtual的,所以完全是可以实现多态的算子的,这样扩展起来更方便了。
...全文
128 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
嗯,比较常见的应用场景之一就是排序回调比较。 其实如果排序是针对class或struct的话,其本身可以是多态的,比较方法以虚方法体现,这样就不需要回调了, 这样可以对A端和B端更好的解耦。
赵4老师 2015-05-19
  • 打赏
  • 举报
回复
Windows系统:“不要调用我,请先填写好未来收到某个消息时你的处理流程,在那个消息到来时我会调用你!” qsort Performs a quick sort. void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) ); Routine Required Header Compatibility qsort <stdlib.h> and <search.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters base Start of target array num Array size in elements width Element size in bytes compare Comparison function elem1 Pointer to the key for the search elem2 Pointer to the array element to be compared with the key Remarks The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: compare( (void *) elem1, (void *) elem2 ); The routine must compare the elements, then return one of the following values: Return Value Description < 0 elem1 less than elem2 0 elem1 equivalent to elem2 > 0 elem1 greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. Example /* QSORT.C: This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */ #include <stdlib.h> #include <string.h> #include <stdio.h> int compare( const void *arg1, const void *arg2 ); void main( int argc, char **argv ) { int i; /* Eliminate argv[0] from sort: */ argv++; argc--; /* Sort remaining args using Quicksort algorithm: */ qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare ); /* Output sorted list: */ for( i = 0; i < argc; ++i ) printf( "%s ", argv[i] ); printf( "\n" ); } int compare( const void *arg1, const void *arg2 ) { /* Compare all of both strings: */ return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); } Output [C:\code]qsort every good boy deserves favor boy deserves every favor good Searching and Sorting Routines See Also bsearch, _lsearch
老王爱上猫 2015-05-19
  • 打赏
  • 举报
回复
对于分享贴必须支持...

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧