What is composition in object-oriented design?
Composition builds an object from other objects that provide needed services, rather than relying primarily on superclass extension.
Study 7 Composition and Object Collaboration with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is composition in object-oriented design?
Composition builds an object from other objects that provide needed services, rather than relying primarily on superclass extension.
What relationship does composition represent?
A has-a relationship means one object contains or uses another object as a part or collaborator, such as a Car having an Engine.
What is object collaboration?
Object collaboration is the interaction among objects that divide a responsibility and request operations from one another.
How does encapsulation support collaboration?
Encapsulation hides internal state and requires other objects to use defined operations instead of accessing that state directly.
Why should a collaborator have a small interface?
A collaborator should expose a small, stable interface so clients depend on meaningful operations rather than unnecessary implementation details.
What is delegation?
Delegation occurs when an object receives a request and passes part of the work to a collaborator that owns that behavior.
When can delegation become harmful?
Delegation is most valuable when the collaborator represents a meaningful, independently replaceable concept; pointless forwarding may indicate unclear boundaries.
What relationship does inheritance represent?
Inheritance represents an is-a relationship in which a subtype should be usable wherever its superclass is expected.
How does composition support independent variation?
Composition lets behavior vary independently by supplying different collaborator implementations, such as interchangeable pay policies.
Why can composition reduce coupling?
Composition can lower coupling because a class usually depends on a collaborator's interface rather than its implementation, protected state, or superclass decisions.
How does composition improve testing?
Composition improves testing by allowing a real external service to be replaced with a small fake or mock, such as a fake PaymentGateway.
How can composition support runtime configuration?
A collaborator can be supplied through a constructor or setter, allowing implementation selection based on configuration, environment, or user choice.