C++中的依赖注入 [英] Dependency injection in C++

查看:29
本文介绍了C++中的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这也是我在 Miško Hevery 的一篇评论中提出的问题 google 谈到 正在处理依赖注入,但它被埋在了评论中.

This is also a question that I asked in a comment in one of Miško Hevery's google talks that was dealing with dependency injection but it got buried in the comments.

我想知道将依赖项连接在一起的工厂/构建器步骤如何在 C++ 中工作.

I wonder how can the factory / builder step of wiring the dependencies together can work in C++.

即我们有一个依赖于 B 的类 A.构建器将在堆中分配 B,在 A 的构造函数中传递一个指向 B 的指针,同时也在堆中分配并返回一个指向 A 的指针.

I.e. we have a class A that depends on B. The builder will allocate B in the heap, pass a pointer to B in A's constructor while also allocating in the heap and return a pointer to A.

事后谁清理?完成后让建筑商清理好不好?这似乎是正确的方法,因为在谈话中它说构建器应该设置预期具有相同生命周期的对象,或者至少依赖项具有更长的生命周期(我也有一个问题).我在代码中的意思:

Who cleans up afterwards? Is it good to let the builder clean up after it's done? It seems to be the correct method since in the talk it says that the builder should setup objects that are expected to have the same lifetime or at least the dependencies have longer lifetime (I also have a question on that). What I mean in code:

class builder {
public:
    builder() :
        m_ClassA(NULL),m_ClassB(NULL) {
    }
    ~builder() {
        if (m_ClassB) {
            delete m_ClassB;
        }
        if (m_ClassA) {
            delete m_ClassA;
        }
    }
    ClassA *build() {
        m_ClassB = new class B;
        m_ClassA = new class A(m_ClassB);
        return m_ClassA;
    }
};

现在,如果有一个依赖项的持续时间预计会超过我们将其注入的对象的生命周期(比如 ClassC 就是那个依赖项),我知道我们应该将构建方法更改为:

Now if there is a dependency that is expected to last longer than the lifetime of the object we are injecting it into (say ClassC is that dependency) I understand that we should change the build method to something like:

ClassA *builder::build(ClassC *classC) {
    m_ClassB = new class B;
    m_ClassA = new class A(m_ClassB, classC);
    return m_ClassA;
}

您的首选方法是什么?

推荐答案

这个演讲是关于 Java 和依赖注入.

This talk is about Java and dependency injection.

在 C++ 中,我们尝试 NOT 来传递 RAW 指针.这是因为 RAW 指针没有与之关联的所有权语义.如果您没有所有权,那么我们不知道谁负责清理该对象.

In C++ we try NOT to pass RAW pointers around. This is because a RAW pointer have no ownership semantics associated with it. If you have no ownership then we don't know who is responsible for cleaning up the object.

我发现大部分时间依赖注入是通过 C++ 中的引用完成的.
在极少数必须使用指针的情况下,将它们包裹在 std::unique_ptr<>std::shared_ptr<> 取决于您希望如何管理所有权.
如果您不能使用 C++11 功能,请使用 std::auto_ptr<> 或 boost::shared_ptr<>.

I find that most of the time dependency injection is done via references in C++.
In the rare cases where you must use pointers, wrap them in std::unique_ptr<> or std::shared_ptr<> depending on how you want to manage ownership.
In case you cannot use C++11 features, use std::auto_ptr<> or boost::shared_ptr<>.

我还要指出,C++ 和 Java 的编程风格现在是如此不同,以至于将一种语言的风格应用于另一种语言将不可避免地导致灾难.

I would also point out that C++ and Java styles of programming are now so divergent that applying the style of one language to the other will inevitably lead to disaster.

这篇关于C++中的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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