Association
Association represents the relationship between two classes. There are two types of association, namely unidirectional association and bidirectional association.
Association | Example |
---|---|
Unidirectional | Customer places an order |
Bidirectional | A is married to B and B is married to A |
Besides, association can also be multiplexed in the form of one-to-one, one-to-many, many-to-one and many-to-many relationships.
Aggregation
Aggregation indicates that when an object that is owned or depend by another object, it can still exist on its own even after the dependent party is dead or ceases to exist.
Examples:
- The relationship between a car and a wheel. The wheel can still serve its purpose when the car it belongs to no longer exist.
- The relationship between a company and an employee. The employee can apply for a new company when the old company goes bankrupt.
This relationship can be understood colloquially as "A uses B, where B can live happily without A. "
Composition
In composition, it is the opposite to the aggregation. The object that is owned by others cannot live on its own.
Examples:
- The relationship between a cup handle and a cup. It is safe to say that each cup comes with different sizes, colors and materials. Hence, the cup handle is unique to a cup and will have no use outside of the cup.
- The relationship between a company and their bank accounts. The bank accounts have no use without the company and would be illegal if someone trying to access the account without legitimate authority.
In other words, composition can be understood as "A owns B and B has no meaning outside A. "
Inheritance
Inheritance describes the relationship between two objects that are usually depicted as a parent and child relationship.
Example:
- The relationship between a cat and animal. The cat is an animal.
"Is a" is the keyword for inheritance.