Describing Composition
- Composition refers to declaring a class within a class
- Composition relates to the composite design pattern
- Specifically, a class known as a composite contains an object of another class known as a component
- Composition is considered as lossely coupled
- Meaning, changes to the component class rarely affect the composite class
- Composition is typically more flexible than inheritance
Comparing Composition, Inheritance, and Aggregation
- In inheritance, class A is a class B
- In composition, class A owns class B
- In aggregation, class A uses class B
- In composition, class B has no meaning or purpose in the system without class A
- In aggregation, class B exists independently from class A
-
For example, suppose we have a
Company,People, andAccountclassCompanyis an aggregation ofPeopleCompanyis a composition ofAccounts- When a
Companyceases to do business, itsAccountscease to exist - However, its
Peoplecontinue to exist in this case
References
Previous
Next