Which of the following best explains why composition is often preferred over inheritance in system design?
Think about flexibility and how behavior can be changed after the system is running.
Composition allows objects to contain other objects and change their behavior dynamically by swapping components. Inheritance fixes behavior in a class hierarchy, making it less flexible.
You are designing a payment system that supports multiple payment methods (credit card, PayPal, crypto). Which design approach best uses composition over inheritance?
Consider how you can change payment methods without changing the main processor class.
Using composition, the PaymentProcessor contains a PaymentMethod component that can be swapped dynamically, allowing flexible payment method changes without modifying the processor.
You have a notification system that sends alerts via email, SMS, and push notifications. To scale and add new channels easily, which design is best?
Think about how to add or remove notification channels without changing existing code.
Composition allows the NotificationSender to hold multiple NotificationChannel components, making it easy to add or remove channels dynamically and scale the system.
Which of the following is a valid tradeoff when choosing composition over inheritance?
Consider the overhead of managing multiple objects versus a class hierarchy.
Composition can introduce more objects and layers of delegation, which may add some performance overhead compared to inheritance's direct method calls.
In a compositional design for a media player, the Player class composes components: Decoder, Renderer, and Logger. Which sequence best describes the request flow when playing a media file?
Think about the logical order of processing from request to output and logging.
The Player first receives the request, then the Decoder processes the file, Renderer outputs the media, and Logger records the event last.