It is always recommended that you design code with a memory-efficient approach to ensure that the memory footprint of the code is lean, and hence, less likely to clog the system. In the case of the Salesforce platform, it is extremely important as Salesforce imposes various limits, such as governor limits, feature limits, and so on. So, it is advisable to keep track of resource consumption.
The flyweight pattern was initially coined and used by Paul Calder and Mark Linton in relation to the What-You-See-Is-What-You-Get (WYSIWYG) visual editor development. As Graphical User Interface (GUI) applications are memory intensive, the flyweight pattern was used to handle memory efficiently.
The flyweight pattern helps reduce the memory footprint of the code and hence is very beneficial in Apex/Visualforce programming. It can help developers reduce the usage of heap memory and viewstate, in turn, avoiding governor limit-related violations. Furthermore, lesser viewstate also means...