- Object Composition (可细分为Composition和Aggregation两部分),
"it allows us to create complex classes by combining simpler, more easily manageable parts."
1.1 Composition与Aggregation的共同点
The part (member) is part of the object (class) . - part-whole relationship
The part (member) does not know about the existence of the object (class).- unidirectional relationship
1.2 Composition与Aggregation的区别
Composition:
- Parts can belong to only one object at a time.
- The whole object is responsible for the existence and lifespan of the parts.
- It models "part-of" relationships.
- In a composition, we typically add our parts to the composition using normal member variables (or pointers where the allocation and deallocation process is handled by the composition class).
Aggregation:
- Parts can belong to more than one object at a time.
- The whole object is not responsible for the existence and lifespan of the parts.
- It models "has-a" relationships.
- In an aggregation, we also add parts as member variables. However, these member variables are typically either references or pointers that are used to point at objects that have been created outside the scope of the class.
- TBD
网友评论