I find code examples best for illustrating, so I'll share an example and try to explain. This is valid Ruby code, but will hopefully read more like pseudo code.
By default, you can grade many types of papers. As long as any new Paper has a grade method, the Teacher can handle it.
The Teacher class has fewer reasons to change now.
I realize this isn't a simple explanation yet, so I'll try to simplify. Dependency injection gives you a Teacher that knows how to grade any paper you assigned to students in the past and any paper you might assign in the future. Without it, the Teacher needs to go back to school whenever a new paper is assigned.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I find code examples best for illustrating, so I'll share an example and try to explain. This is valid Ruby code, but will hopefully read more like pseudo code.
Without dependency injection:
Teacher
.Paper
change, you need to updateTeacher
.Teacher
class has many reasons to change.With dependency injection:
Paper
has agrade
method, theTeacher
can handle it.Teacher
class has fewer reasons to change now.I realize this isn't a simple explanation yet, so I'll try to simplify. Dependency injection gives you a Teacher that knows how to grade any paper you assigned to students in the past and any paper you might assign in the future. Without it, the Teacher needs to go back to school whenever a new paper is assigned.