Object composition 00:00:00 Make a prediction You will build independently replaceable parts, state the unit contract between them, and test the assembled object with one substitute. Indoor and outdoor planners both work with wheels or tracks. If we add one drone drive, do we need separate copies of both planning algorithms? No. Give the robot a planner and a drive separately. One new compatible drive can use either planner, provided the route format and units match. 00:00:29 Sam and the cleaner A cleaner holds a motor, a filter and a nozzle. Changing the nozzle for upholstery does not require replacing the motor. But a nozzle whose fitting does not match the hose cannot work merely because both pieces came in the same box. Composition gives objects independent variation. It still requires a precise contract at each joint, including formats, units and failure behaviour. Object composition means that an object gets its behaviour from other objects that it holds. A robot does not know how to fly. It has a movement part, and that part knows. Things that vary independently multiply when they share one class tree. Three ways to move, three sensors and three radios give 27 robots. As parts they give three plus three plus three. 00:01:23 Mix a route with a power source A delivery robot needs a route planner and a drive unit. Indoor and outdoor planners can each work with wheeled or tracked drives. Give the robot those two collaborators independently. The planner supplies the path and the drive executes the movement. Replace just the planner in a test and check the drive receives the new path. Four combinations no longer require four subclasses plus a new family for each extension. Give the robot a route one metre long and plug in a replacement drive unit. If both parts agree on metres, the new unit can move the same distance with different hardware. If the plug fits but the replacement interprets centimetres, composition alone has not preserved the contract. 00:02:07 Account for the contract between parts A robot's planner emits metres while a replacement drive reads centimetres. Composition alone does not fix the mismatch. Specify the unit at the collaboration boundary, then adapt or reject the replacement. Swapping an object is safe only when it keeps the promised behaviour. At the boundary, convert one metre to one hundred centimetres and verify the movement. Now send zero. A good adapter preserves that boundary too. Checking only that two types expose a move method would have missed a hundredfold distance error. A robot whose planner emits metres but whose drive consumes centimetres can silently travel one hundredth the right distance. Passing an interface check may not catch that. If a variation genuinely must keep all of an existing role's public promises, a subtype may express that relationship more clearly than wrapping every operation. React. Its earlier documentation recommends composition instead of inheritance to reuse code between components, and says that Facebook found no case for component inheritance in thousands of components. Unity. A GameObject is a container. The manual calls components the functional pieces of every GameObject. 00:03:26 Try an interview follow-up How do you exchange a part under load? Read the reference once per use, replace it in one atomic step, and close the old one only when its last user has left, with a time limit. Counting users or shared pointers does that. Assign and close breaks running work. A membership renewal checks the current date. Give it a clock and set a test clock to one minute before expiry. Advance it by two minutes and repeat. The same renewal object now demonstrates both outcomes without waiting. Explain who owns the clock and whether several renewals may share it. 00:04:02 What to take with you Find the choices that vary on their own. Give each a small promise and a family of parts. Let the whole hold one part of each family and only coordinate. Wire in one place, keep rules between parts in one place, and use a null part instead of an empty field. Swap a part only between two uses of it.