0
0
LLDsystem_design~12 mins

Flyweight pattern in LLD - Architecture Diagram

Choose your learning style9 modes available
System Overview - Flyweight pattern

The Flyweight pattern helps save memory by sharing common parts of objects instead of creating many copies. It is useful when many similar objects are needed, like characters in a text editor or icons in a UI. The key is to separate shared data (intrinsic) from unique data (extrinsic) to reduce memory use.

Architecture Diagram
User
  |
  v
Flyweight Factory <--> Shared Flyweight Objects
  |
  v
Unique Object Data (Extrinsic State)
  |
  v
Client uses Flyweight + Extrinsic Data
Components
User
actor
Initiates requests to create or use objects with shared data
Flyweight Factory
service
Manages and provides shared flyweight objects to clients
Shared Flyweight Objects
shared_object
Contains intrinsic (shared) state reused by many clients
Unique Object Data (Extrinsic State)
data_store
Stores unique data passed by client to complete object behavior
Client
component
Combines shared flyweight and extrinsic data to perform operations
Request Flow - 5 Hops
UserFlyweight Factory
Flyweight FactoryShared Flyweight Objects
UserUnique Object Data (Extrinsic State)
ClientShared Flyweight Objects
ClientUser
Failure Scenario
Component Fails:Flyweight Factory
Impact:Cannot provide shared flyweight objects, leading to object duplication and increased memory use
Mitigation:Implement caching with fallback to recreate shared objects; use persistent storage or backup factory instance
Architecture Quiz - 3 Questions
Test your understanding
What is the main purpose of the Flyweight Factory in this pattern?
ATo perform operations combining intrinsic and extrinsic data
BTo store unique extrinsic data for each object
CTo create and manage shared flyweight objects
DTo act as the user interface for clients
Design Principle
The Flyweight pattern reduces memory use by sharing common intrinsic state among many objects, separating it from extrinsic state that varies per instance. This design is efficient for systems with many similar objects, improving scalability and performance.