Dependency
Temporary use - weakest relationship between classes.
Dependency is the weakest relationship where one class uses another temporarily. The dependent class doesn’t store a reference to the other class - it only uses it as a parameter, local variable, or return type.
What is Dependency?
Section titled “What is Dependency?”Dependency represents:
- “Uses temporarily” relationship
- No ownership - doesn’t store reference
- Temporary use - only during method execution
- Weakest relationship - no coupling beyond method call
Key Characteristics
Section titled “Key Characteristics”- Temporary use - Only during method execution
- No storage - Not stored as instance variable
- Method parameters - Usually passed as parameters
- Dashed arrow in UML diagrams
Basic Dependency Example
Section titled “Basic Dependency Example”Visual Representation
Section titled “Visual Representation”Real-World Example: Payment Processing
Section titled “Real-World Example: Payment Processing”Dependency vs Association
Section titled “Dependency vs Association”| Feature | Dependency | Association |
|---|---|---|
| Duration | Temporary | Persistent |
| Storage | Not stored | Stored as reference |
| Lifecycle | No coupling | Some coupling |
| UML Symbol | Dashed arrow | Solid arrow |
| Example | Order uses Calculator | Teacher has Courses |
Key Takeaways
Section titled “Key Takeaways”When to Use Dependency
Section titled “When to Use Dependency”Use Dependency when:
- Class uses another temporarily
- Passing objects as method parameters
- Using objects as local variables
- You want minimal coupling
- Relationship is not persistent
- Objects are created externally
Examples:
- Order uses Calculator (for calculation)
- Order uses Printer (to print receipt)
- ShoppingCart uses PaymentProcessor (to process payment)
- Report uses Formatter (to format output)
- Service uses Logger (to log messages)