High level constructor injection configuration in C ++

My questions specifically relate to dependency injection through the constructor. I understand the pros / cons of the service locator template, the constructor / setter installation and their tastes, however there is something that I cannot miss after choosing a clean constructor injection. After reading many materials for the tested design, including a thorough study of the blog Miško Hevery (in particular this post), I am in the following situation:

Suppose I am writing a C ++ program, and I entered my dependencies correctly through my constructors. For readability, I gave myself a high-level object that has one Execute () function called from main:

int main(int argc, char* argv[]) {
    MyAwesomeProgramObject object(argc, argv);
    return object.Execute();
}

To fulfill () the responsibility is to simply connect all the necessary objects and run the object of the highest level. The highest level object requires two dependencies, and these objects require several objects, etc. Etc., implying a function that looks like this:

MyAwesomeProgramObject::Execute() {
    DependencyOne one;
    DependencyTwo two;
    DependencyThree three;

    MidLevelOne mid_one(one);
    MidLevelTwo mid_two(two, three);

    // ...

    MidLevelN mid_n(mid_dependencyI, mid_dependencyJ, mid_dependencyK);

    // ...

    HighLevelObject1 high_one(mid_one, mid_n);
    HighLevelObject2 high_two(mid_two);

    ProgramObject object(high_one, high_two);
    return object.Go();
}

From what I take from Mishko’s blog (and I would ask him, but decided that he won’t have time to get back to me), this is the only way to satisfy the purely design injection of dependencies.

In a blog post said , he claims that we should have factories on the living standards of each object, but this is essentially what Execute does, which makes my code look like its example:

AuditRecord audit = new AuditRecord();
Database database = new Database(audit);
Captcha captcha = new Captcha();
Authenticator authenticator =
    new Authenticator(database, captcha, audit);
LoginPage = new LoginPage(audit, authenticator);

Questions:

  • Is this the right approach?
  • Is this a template that I don't know about (similar to Maven context.xml)?
  • "" ?
+4
1

, . , .

, . , , , , , ... , . , .

, , .

std::shared_ptr<foo> foo_object(new foo);
std::shared_ptr<blah> foo_object(new blah(foo));

, blah foo , , , . , NULL (, ). , ...

, , , . , , . , , (.. , , 100% , !)

0

All Articles