Flyweight 00:00:00 Make a prediction You will separate reusable intrinsic data from per-instance state and choose a key that is safe to share. Ten thousand trees use three species images. If one tree sways in the wind, should updating the shared species texture move every tree of that species? No. Share immutable species artwork; store position and individual animation state outside it. Otherwise a change meant for one tree leaks into every instance sharing the same representation. 00:00:33 Asha prints a forest A printer uses one carved stamp for all pine trees on a map and places it at different coordinates. Moving the stamp to a new location does not slide the older ink marks across the paper. The stamp represents reusable intrinsic data. Location and scale belong to each draw request. Leaving wet paint on the stamp would accidentally affect the next tree. Flyweight is a structural design pattern that saves memory by sharing. Objects that would be equal in large parts refer to one shared object for that part. Programs often hold thousands or millions of small things that are nearly alike: letters in a text, trees in a game, cells in a table, rows with the same city name. 00:01:17 Draw a forest with shared artwork A map holds ten thousand trees but only three species sprites. Storing a separate texture with each tree wastes memory. Keep position and scale on each tree instance, and share immutable species artwork by key. The draw call combines the instance coordinates with that artwork. Move one tree and verify only its coordinates change. If wind bends one individual tree, that per-tree state must also stay outside the shared object. Place two oaks at different coordinates using the same texture asset. Move only the first tree and redraw. If both move, position leaked into their shared state; if neither shares the texture, the memory win disappeared. Count shared types separately from placed instances. 00:02:06 Share a glyph without sharing its location A page can reuse a font glyph outline for every occurrence of the same letter and font style. Each occurrence still needs its own position. Include the style in the shared key or the bold and regular letters may accidentally use the same geometry. Draw a regular A twice and a bold A once. The two regular instances can share their outline while their x-coordinates stay independent. The bold instance needs a different style key. Omitting the weight from that key would save space by drawing the wrong glyph. If the sharing key omits font weight, bold and regular glyphs may accidentally share the wrong outline. Include every intrinsic distinction required for correct rendering. If instances rarely repeat or contain mostly unique data, the lookup and management costs can outweigh the memory savings. Java can reuse boxed integer objects for a defined small range, including minus one hundred and twenty-eight through one hundred and twenty-seven. Other values may also be cached. Comparing object identity can therefore give a misleading impression of numeric equality. Always choose comparison according to the values the application means to compare. 00:03:24 Try an interview follow-up How would you cut the memory of 40 million rows with city names? I would measure how many distinct city names occur across the forty million rows. If many repeat, store one dictionary and represent each row by a small code. I would also measure lookup cost and decide how dictionaries merge across files. A column with almost all unique values gives this approach little to share. One page contains two thousand copies of the same letter at different positions. Share the immutable glyph shape for that font and size. Keep each position with the layout. Move one letter and verify that only its placement changes. Include font size in the shared key so two sizes cannot reuse the wrong shape. 00:04:14 What to take with you Split the state: what is true for all oaks goes into one immutable TreeType, what is true for this oak stays in a small Tree and is passed to draw. A factory makes sure that there is one type per key. The key must be exactly the intrinsic state. Too fine a key shares nothing, too coarse a key mixes things up. Test identity and the number of loads, because a factory that shares nothing still draws the right picture.