在C ++中使用回调是否增加耦合? [英] Does using callbacks in C++ increase coupling?

查看:122
本文介绍了在C ++中使用回调是否增加耦合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Q1。为什么使用回调函数?



Q2。回调是否邪恶?乐趣为那些谁知道,对于别人一个噩梦的



Q3。任何替代回调?



解决方案

无论使用回调在C ++增加耦合 ,我建议使用事件处理程序样式,特别是处理事件类的东西。例如,具体的一个设计模式,而不是回调概念如下:

  class MyClass 
{
public:
virtual bool OnClick(...)= 0;
virtual bool OnKey(...)= 0;
virtual bool OnTimer(...)= 0;
virtual bool OnSorting(...)= 0
...
};

你可能仍然认为上述函数是回调,但是当把它们当作已知的设计模式时,因为你正在做OO和编写C ++。



Effo UPD @ 2009nov13 -
典型案例:框架,事件系统或并发编程模型,等等。以下示例应该是有用的。



Framework控制整体流程,好莱坞原则声明不要打电话给我们,我们会打电话给你。 (这就是回调的意思是精确的)而每一个正常的函数或lib,调用者控制流。



一个众所周知的C框架是Linux内核,Linux驱动程序编写者知道他/他将在其中实现一个struct file_operations OnRead()和write()意味着OnWrite()等。

  struct file_operations {
struct module * owner ;
loff_t(* llseek)(struct file *,loff_t,int);
ssize_t(* read)(struct file *,char __user *,size_t,loff_t *);
ssize_t(* write)(struct file *,const char __user *,size_t,loff_t *);
ssize_t(* aio_read)(struct kiocb *,const struct iovec *,unsigned long,loff_t);
ssize_t(* aio_write)(struct kiocb *,const struct iovec *,unsigned long,loff_t);
...
};

但是框架最简单的例子应该是:

 框架|一个开发者要做
---------------------------- + ------------- -------------------------
| class MyClass:public Actor
| {
| public:
pApplication-> Init(...); | virtual bool OnInit(...){}
pApplication-> Run(...); | virtual int OnRun(...){}
pApplication-> Quit(...); | virtual void OnQuit(...){}
| ...
| }; pApplication-> Init()将调用pActor-> OnInit和pApplication-> Run()函数,其中pApplication-> Init()在内部调用pActor-> OnRun(),等等。大多数Windows GUI开发人员经历过实现OnMouseClick()或OnButtonPress()等。



我同意这个线程的其他答案,他们根据观点给出正确的解释,例如分层方法中的处理程序,广义回调或异步操作等。这取决于你有什么想法适合你。


Q1. Why are callback functions used?

Q2. Are callbacks evil? Fun for those who know, for others a nightmare.

Q3. Any alternative to callback?

解决方案

Regardless of "using callbacks in C++ increase coupling" or not, I suggest using event handler style especially process event-like things. For example, concrete a Design Pattern instead of callback concept as the below:

class MyClass
{
public:
    virtual bool OnClick(...) = 0;
    virtual bool OnKey(...) = 0;
    virtual bool OnTimer(...) = 0;
    virtual bool OnSorting(...) = 0
    ...
};

You may still consider the above functions are callbacks, but when considering them as the known Design Pattern, you won't get confused because you are doing OO and writing C++.

Effo UPD@2009nov13 - Typical cases: Framework, Events System or Concurrent Programming Model, and so on. Following examples should be useful.

Framework controls the overall flow as the Hollywood Principle states "Don't call us, we'll call you." (that's what "callback" means exactly) while per a normal function or lib, caller controls the flow.

A well-known C framework is Linux kernel and a Linux driver writer knows that s/he'd implement a "struct file_operations" in it "read()" means OnRead() and "write()" means OnWrite() etc.

struct file_operations {
        struct module *owner;
        loff_t (*llseek) (struct file *, loff_t, int);
        ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
        ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
        ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
        ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
        ...
};

But the simplest example of a framework should be:

    The Framework           |     A developer to do
----------------------------+--------------------------------------
                            |    class MyClass : public Actor
                            |    {     
                            |    public:
pApplication->Init(...);    |        virtual bool OnInit(...) {}
pApplication->Run(...);     |        virtual int OnRun(...) {}  
pApplication->Quit(...);    |        virtual void OnQuit(...) {}
                            |        ...
                            |    };

and pApplication->Init() will call pActor->OnInit, and pApplication->Run() calls pActor->OnRun(), and so on internally. Most Windows GUI developers had experienced implementing OnMouseClick() or OnButtonPress() etc.

I agree with other answers of this thread that they give correct explanation based on the viewpoint accordingly, such as handlers in layered approach, generalized callback or aynchronous operations, and so on. It's up to you that what idea(s) would be suitable for you.

这篇关于在C ++中使用回调是否增加耦合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆