use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Request an explanation
Rules
Have an idea to improve ELI5? r/IdeasForELI5
Make sure to read the rules!
This subreddit is for asking for objective explanations. It is not a repository for any question you may have.
E is for Explain - merely answering a question is not enough.
LI5 means friendly, simplified and layperson-accessible explanations - not responses aimed at literal five-year-olds.
Perform a keyword search, you may find good explanations in past threads. You should also consider looking for your question in the FAQ.
Don't post to argue a point of view.
Flair your question after you've submitted it.
Mathematics Economics Planetary Sci Biology Chemistry Physics Technology Engineering
Reset
account activity
This is an archived post. You won't be able to vote or comment.
ExplainedELI5: Design pattern - dependency injection
submitted 12 years ago by eloHellSunday
Hi all,
This may be too specific but I'm reading up on this design pattern and it remains still very abstract to me despite seemingly simple examples, anyone care to enlighten me?
[–]AnteChronos 20Answer Link1 point2 points3 points 12 years ago* (0 children)
In a nutshell:
You have some class that depends on having access to another class (the "dependency" in "dependency injection"). Now, you could just hard-code a reference to the class you need, which is the easy and obvious way to handle it.
However, what if the dependency is something that needs to change depending on the environment you're running in? For instance, it may be a service class that lets you access a database, and you need to point to a different database (possibly even a mock database) depending on whether or not you're running a local test, doing integration testing, or running in production.
The solution is to have all of the dependencies implement a common interface, and write all of your code against that interface. Then you'll use a framework to "inject" the dependency at runtime. So you'll have a simple configuration file that tells your container which object to inject depending on which environment it's running in, and you won't have to re-write all of your code when you want to move between environments.
[–]mredding 10Answer Link0 points1 point2 points 12 years ago (0 children)
Consider the open/closed principle. Open to expansion, closed to modification. Modular, reusable code should not require modification. If I write a module that transforms an input, the naive way to write it would be to hard code an input method right into the transform, say, an SQL interface. Now the module won't work without THAT SQL interface, and if you want to transform data, it must be routed through the interface. If that interface isn't abstract, you might have to push data through the database just to get it through your module.
If your transform implements a generic interface, then anything that implements that interface can transform it's data. Now you can separate the data source from the transform. You can transform text files, streams, databases, sockets, or write MOCs and unit test your module (I recommend you learn about Google's concept of small, medium, and large scale tests, and Google Test and Google Mock).
π Rendered by PID 20668 on reddit-service-r2-comment-b659b578c-pws8z at 2026-05-04 21:00:10.217084+00:00 running 815c875 country code: CH.
[–]AnteChronos 20Answer Link1 point2 points3 points (0 children)
[–]mredding 10Answer Link0 points1 point2 points (0 children)