What are Dependencies, Composition, Delegation, and Aggregation in Object-Oriented Programming?

Last updated May 31st, 2022
The four principles of object-oriented programming (abstraction, inheritance, encapsulation, and polymorphism) are features that - if used properly - can help us write more testable, flexible, and maintainable code.

Dependency: The Address object comes from outside, it's allocated somewhere else. This means that the Address and Employee objects exists separately, and only depend on each other.

Composition: Here you see that a new Engine is created inside Car. The Engine object is part of the Car. This means that a Car is composed of an Engine.

Composition is about the relationships between objects.

Delegation is about passing work from one object to another.

These are actually different (but sometimes related) concerns.

What you've got is B composed of A (B refers to A).

Aggregation & composition: An aggregation is an object that represents a concept as a whole and handles managing how its parts are accessed and changed. Composition is related but it adds an additional meaning to aggregation. Composition says that parts of the aggregation can’t exist independently (ie: a wheel can’t exist unless it belongs to a car).

So dependency == aggregation? stackoverflow.com/questions/11881552/… – danihodovic Jan 9, 2014 at 15:04 @dani-h No, aggregation and composition describe how things are build/structured, while dependency is more a property of a certain structure. See @TheLostMind's great answer



Discussion

Liked this? Sing it loud and proud πŸ‘¨β€πŸŽ€.


0 Comments

Be the first to leave a comment

Submit

Stay in touch!



About the author

Khalil Stemmler,
Developer Advocate @ Apollo GraphQL ⚑

Khalil is a software developer, writer, and musician. He frequently publishes articles about Domain-Driven Design, software design and Advanced TypeScript & Node.js best practices for large-scale applications.



View more in Object-Oriented Programming



You may also enjoy...

A few more related articles

4 Principles of Object-Oriented Programming
The four principles of object-oriented programming (abstraction, inheritance, encapsulation, and polymorphism) are features that -...
Object Stereotypes
The six object stereotypes act as building blocks - stereotypical elements - of any design.
Non-Functional Requirements (with Examples)
Non-functional requirements are quality attributes that describe how the system should be. They judge the system as a whole based ...
How to Test Code Coupled to APIs or Databases
In the real-world, there's more to test than pure functions and React components. We have entire bodies of code that rely on datab...

Want to be notified when new content comes out?

Join 15000+ other developers learning about Domain-Driven Design and Enterprise Node.js.

I won't spam ya. πŸ–– Unsubscribe anytime.

Get updates