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

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

问题描述

第一季度.为什么要使用回调函数?

Q1. Why are callback functions used?

第 2 季度.回调是邪恶的吗?对那些人来说很有趣谁知道呢,对其他人来说是一场噩梦.

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

第三季度.有什么替代回调的方法吗?

Q3. Any alternative to callback?

推荐答案

无论在 C++ 中使用回调会增加耦合"与否,我都建议使用事件处理程序风格,尤其是处理类似事件的事情.例如,具体一个设计模式而不是回调概念如下:

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
    ...
};

你可能仍然认为上面的函数是回调函数,但是当把它们看作已知的设计模式时,你不会因为你是在做 OO 和写 C++ 而感到困惑.

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++.

Efo UPD@2009nov13 -典型案例:框架、事件系统或并发编程模型等.下面的例子应该很有用.

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.

一个著名的 C 框架是 Linux 内核,Linux 驱动程序编写者知道他/她会在其中实现一个struct file_operations"read()"表示 OnRead() 和write()"表示 OnWrite() 等

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(...) {}
                            |        ...
                            |    };

和 pApplication->Init() 会调用 pActor->OnInit,而 pApplication->Run() 会在内部调用 pActor->OnRun(),依此类推.大多数 Windows GUI 开发人员都有过实施 OnMouseClick() 或 OnButtonPress() 等的经验.

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天全站免登陆