Flyweight Pattern in Spring Boot — Render SVG Icons Efficiently (With Caching)
D
direct_x_34
Copy Paster!
What you’ll learn:
- How to stop recreating the same “heavy” objects on every request (SVG paths/templates/configs)
- Flyweight split: intrinsic (shared) = icon type + SVG path, extrinsic (per request) = color + size
- One endpoint, many variants: GET `/api/icons?type=CART&color=%23ff0000&size=32`
- Flyweight Factory + ConcurrentHashMap cache to guarantee “one instance per icon type”
- Verify caching: Use /api/icons/cache-size to see how many icon types are cached (e.g., 3 types → 3 flyweights)
For more details, check out the Medium link below — this article walks through the whole implementation step by step.
Link: https://sngermiyanoglu.medium.com/flyweight-pattern-in-spring-boot-render-svg-icons-efficiently-with-caching-1455370b6536
