Proxy & Flyweight
You'll learn to
- -Use Proxy as a stand-in with the same interface that adds caching, lazy loading or a permission check
- -Use Flyweight to share the identical part of many objects, separating shared (intrinsic) from per-use (extrinsic) data
- -Count the savings in both patterns and know when each is worth its complexity
Both patterns are about using resources sensibly, but from different angles. Proxy controls access to one object by standing in front of it. Flyweight saves memory by sharing the identical part of many objects instead of copying it into each.
Proxy: A Stand-In With the Same Interface
Picture a personal assistant who answers your phone. Callers talk to the assistant exactly as they would to you. The assistant can screen calls, take a message to deal with later, or remember an answer you already gave so you are not asked twice. A proxy is that assistant for an object. It offers the same interface as the real object, so callers cannot tell the difference, and it adds something before passing the call on.
- -A virtual proxy delays creating an expensive object until the moment it is really needed (lazy loading).
- -A caching proxy remembers the result of an expensive call and returns it on repeats.
- -A protection proxy checks permissions before it lets a call through.
In the demo, loading an image from disk is slow, so a caching proxy sits in front of the loader. We ask for three images, one of them twice, and count the real disk reads.
Flyweight: Sharing the Identical Part
Think of a printer's tray of metal type. There is one metal "e" stamp, and it prints every "e" on the page. Nobody carves a new stamp for each letter. Flyweight applies when you need a huge number of similar objects and most of their data is the same. The pattern splits each object's data in two. Intrinsic state is shared, never changes and is stored once. Extrinsic state is different for every use and is passed in when needed.
In a text document, the glyph (the drawn shape of a character in a font, with its heavy font data) is intrinsic. Where that character sits on the page is extrinsic. The demo builds the positions for an 11,000-character text and counts how many glyph objects it really needed.
To see why that matters, assume each glyph carries about 2 KB of font data. Giving each of the 11,000 positions its own copy would cost around 22 MB. Sharing 27 glyphs costs about 54 KB, a drop of roughly 400 times. The position numbers are the only per-character data left.
Flyweight is a memory optimization, and optimizing early is a trap here too. Reach for it only when you can name the pressure, such as millions of near-identical objects. For a few dozen objects the savings would not be worth the extra structure.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!