Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:10:33+00:00 2026-05-27T22:10:33+00:00

What do they mean with Dependency Injection (Inversion of Control) in this context (

  • 0

What do they mean with Dependency Injection (Inversion of Control) in this context (Google Mock):

Let’s look at an example. Suppose you are developing a graphics
program that relies on a LOGO-like API for drawing. How would you test
that it does the right thing? Well, you can run it and compare the
screen with a golden screen snapshot, but let’s admit it: tests like
this are expensive to run and fragile (What if you just upgraded to a
shiny new graphics card that has better anti-aliasing? Suddenly you
have to update all your golden images.). It would be too painful if
all your tests are like this. Fortunately, you learned about
Dependency Injection and know the right thing to do: instead of having
your application talk to the drawing API directly, wrap the API in an
interface (say, Turtle) and code to that interface
:

class Turtle {
  ...
  virtual ~Turtle() {}
  virtual void PenUp() = 0;
  virtual void PenDown() = 0;
  virtual void Forward(int distance) = 0;
  virtual void Turn(int degrees) = 0;
  virtual void GoTo(int x, int y) = 0;
  virtual int GetX() const = 0;
  virtual int GetY() const = 0;
};

What does it have to do with DI when adding another layer between application code and drawing API by a class? In many Java example about Dependency Injection, usually an object should not be concretely created inside a class. Rather, it should be created elsewhere to separate the implementation coupling between two objects. For example (source from codeproject):

enter image description here

Solution:

enter image description here

As I searched through the answers about DI on Stackoverflow, usually it is asked in the context of Java. Some example used the Java GUI. Usually the examples is so simple and so obvious that I failed to see its significance, except for having better design with less coupling. However, what I want to learn is the meaning behind it. As defined in wiki, Inversion of Control (IoC) means you invert the flow of control of code. So, how does it apply to the Google case? How the actual flow is inverted compare to procedural style? I thought that code is executed sequentially line by line from top to bottom, not from bottom to top?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:10:33+00:00Added an answer on May 27, 2026 at 10:10 pm

    Wikipedia has good definitions of these:

    1. http://en.wikipedia.org/wiki/Inversion_of_control
    2. http://en.wikipedia.org/wiki/Dependency_injection

    Dependency Injection is just a fancy way of saying that the class interacts with another via an interface (the Graphics API) and that it provides a way of changing what the interface points to (i.e. injecting a dependency on another class).

    For Inversion of Control, Wikipedia mentions things like the Factory pattern.

    It also mentions setter injection (changing an interface implementation using a setter function), construction injection (setting the interface implementation from the constructor) or interface injection (requesting an interface implementation from another interface) and notes that these are types of Dependency Injection.

    This is what is happening here — the program can change the drawing API of the turtle program (Dependency Injection) using a setter method (Inversion of Control).

    This allows you to have a test class like this:

    struct DrawingTester : public DrawingInterface
    {
        void move_to(long x, long y) { printf("moveto %d %d\n", x, y); }
        void line_to(long x, long y) { printf("lineto %d %d\n", x, y); }
    };
    

    and drive it through a test program:

    int main(int argc, char **argv) {
        DrawingTester drawing;
        Turtle t;
        t.setDrawingApi(&drawing);
        t.runProgramFromFile(argv[0]);
        return 0;
    }
    

    You can then have the turtle/logo test programs with expected output from the DrawingTester. For example:

    # test1.logo
    
    MOVE 5, 7
    
    # test1.calls
    
    moveto 5 7
    

    and drive this through a test suite (e.g. https://github.com/rhdunn/cainteoir-engine/blob/master/tests/harness.py and https://github.com/rhdunn/cainteoir-engine/blob/master/tests/metadata.py).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example in $(CC) $(CFLAGS) -c -o $@ $< what do they mean?
I got this c++ macro and wonder what they mean by code%2 (the percentage
Possible Duplicate: What is Inversion of Control? Okay, I'm new to this site and
has anyone gotten these ms-access errors? what do they mean? im connecting access with
I'm really beginning to understand what people mean when they say that C++'s error
I mean not on open source, but software to build and sell. They don't
concerning Gmail labels - what are they technically speaking. I mean through imap connection
whenever they say run a .class file in JVM. do they mean with the
I have been looking at various dependency injection frameworks for .NET as I feel
There are two source codes of android, but I don't understand what they mean.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.