Flyweight Pattern
The Flyweight design pattern is a structural pattern designed to minimize memory usage by sharing as much data as possible with other similar objects. It divides an object's data into two categories: intrinsic state(heavy, constant, and shareable data) and extrinsic state (lightweight, unique, context-specific data) #include <iostream> #include <string> #include <string_view> #include <unordered_map> #include <vector> #include <memory> #include <shared_mutex> // 1. INTRINSIC STATE (The Flyweight) // This class contains heavy, immutable data shared across thousands of instances. class TreeModel { public: TreeModel(std::string name, std::string color, std::vector texture_data) : name_(std::move(name)), color_(std::move(color)), texture_bytes_(std::move(texture_data)) { std::cout texture_bytes_; // Simulates heavy mesh/texture data }; // 2. FLYWEIGHT FACTORY // Manages the shared pool of flyweights. It ensures obje...