
TypeScript 4 Design Patterns and Best Practices
By :

The last structural design pattern you will learn about in this chapter is Flyweight. This pattern deals with managing efficient usage of memory or space resources by allocating some of the objects internally. At times, when you frequently use objects such as strings or cache values from numerous clients, it becomes enormously expensive to generate them on the fly every time. With this pattern, you provide an interface, so the client can still benefit from using those objects but share them as well as much as possible behind the scenes.
One analogy of this pattern is sharing a few traditional costumes among many dancers. Because those costumes are very expensive to buy sometimes, some of the dancers may have to buy new ones but some may distribute them between performances. The manager, for example, takes the role of the Flyweight and decides when they need to purchase new ones or share existing ones. Justifying why and how you should conserve memory resources...