Historical Context
Before object-oriented programming became popular, software was primarily procedural. Programs looked something like this:
main()
↓
Read File
↓
Process Data
↓
Print ResultEverything happened sequentially.
Functions simply called other functions.
As software became larger, engineers realized something interesting.
Even though functions weren't storing other functions they still depended on them.
Example:
calculateSalary()
{
readEmployee();
calculateTax();
saveSalary();
}Here,
calculateSalary()
depends on
readEmployee()
calculateTax()
saveSalary()If any of those functions changed significantly, the caller could break.
This idea eventually evolved into dependency.
When object-oriented programming emerged, functions became methods and data became objects.
Why Does Dependency Exist?
Imagine a restaurant.
There are:
- Chef
- Waiter
- Customer
- Cashier
- Delivery Person
Does the chef own the waiter?
No.
Does the chef contain the waiter?
No.
Does the waiter belong to the chef?
No.
The chef simply needs the waiter to deliver food.
Once the food leaves the kitchen, their interaction is finished.
This is dependency.
The chef uses the waiter. Nothing more.
Software works the same way.
An object often needs another object only while performing a task. It does not permanently own it.
First Principles
Let's forget programming. Imagine building a house.
You need:
- Hammer
- Drill
- Saw
- Measuring tape
Do you become permanently connected to the hammer?
No.
You simply use it. After finishing, the hammer goes away.
You relationship with the hammer is temporary.
This is exactly what dependency represents.
Dependency answers only one question:
“Who needs whom to accomplish a task?”
Not
- Who owns whom?
- Who contains whom?
- Who manages lifetime?
Only
Who temporarily uses whom?
Formal Definition
A dependency exists when
One software element requires another software element in order to perform some operation, but does not own or permemently maintain it.
The key word is requires.
Not:
- contains
- owns
- manages
- stores
- inherits
- just
- requires
The Mental Model
Imagine people in an office.
Manager
asks
Accountant
↓
Generate Report
↓
Accountant returns report
↓
Interaction endsThe manager depends on the accountant.
But the manager does not own the accountant.
This is dependency.
The Simplest Example
class Printer
{
public:
void print()
{
std::cout << "Printing...\n";
}
};Now,
class Student
{
public:
void submitAssignment(Printer& printer)
{
printer.print();
}
};Question: Does Student own Printer?
No.
Student merely needs Printer temporarily.
As soon as the function finishes, the dependency disappears.
Leave a comment
Your email address will not be published. Required fields are marked *
