Prototype 00:00:00 Make a prediction You will specify what is copied, what stays shared and what receives a new identity when an object is cloned. A new poster copies a template's list of mutable text boxes by reference. You edit the clone's headline. Could the original template change too? Yes. A new top-level poster does not isolate shared mutable text boxes. Copy the pieces whose changes must be private, and share only what the contract permits. 00:00:31 Asha and the strawberry mothers A strawberry plant sends out a runner. The daughter starts with the mother's variety, but planting it in a separate pot lets you water it independently. Leaving two plants in the same flood tray is a different sharing decision. A prototype establishes starting structure. Decide explicitly which references remain shared, which become independent and which identities are fresh. Prototype is a creational design pattern. You keep an object that is already right, the prototype, and make new ones by asking it to copy itself. Some objects are expensive to build: they read files, ask a database, or calculate for seconds. Copying what is in memory costs far less, as long as the copy is cheaper than the build. 00:01:16 Clone a reusable drawing A poster template contains colours, a headline and a list of text boxes. A new poster should begin with the same arrangement. Copy the prototype and edit the new headline. Immutable colour descriptions can be shared, but mutable text boxes need independent ownership. Change the clone's first box and inspect the original. If both change, the copy shares more mutable state than the contract allows. Clone the spring drawing, rename the copy summer and move one of its shapes. Open spring again. The old label alone does not prove the clone is independent: the shape position must still match spring too. Immutable shape styles may safely be shared. 00:02:00 Decide what identity means Cloning a game character template should copy its starting abilities, while the spawned character receives its own identity and health. An active file handle or live network connection may be impossible or wrong to duplicate. Specify the copy rules per field instead of blindly deep-copying everything. Clone a game character with two abilities, give the clone a third, and compare both ability sets. They should now differ. If your identity store treats the clone as the original character, saving it could overwrite the source. Decide identity separately from copied configuration. Blindly copying a live network connection or external identifier can produce an invalid duplicate. Copying deeper is not always safer. A factory that constructs a clean object is often clearer when little useful starting state can be reused. Python provides shallow and deep copies. A shallow copy creates a new outer object while retaining references to its contents. A deep copy traverses nested objects and remembers what it already copied, which helps with cycles and repeated references. Custom copy behaviour is useful when a resource should remain shared or should never be duplicated. 00:03:18 Try an interview follow-up A subclass adds a field and the copy loses it. How do you prevent that? Each class copies its own fields and chains to its parent. A final copy method checks that the copy has the same class. One generic test over all classes compares values and finds shared changeable parts. Duplicate an event invitation and change one guest on the duplicate. If the original guest list changes, the copy shared mutable state by accident. Decide which fields may be shared, such as an immutable template, and which need a new collection or identity. Test both copies after editing one. 00:04:00 What to take with you Prototype lets an object copy itself. The caller needs no class name, private fields stay private, and expensive work is done once. A registry names prepared prototypes and copies on the way in and on the way out. Deep or shallow is decided for each field: share what never changes, copy what does, renew what identifies, reset what belongs to the past. Cycles and shared parts need a map from original to twin that is filled before links are followed.