What is the Flyweight Design Pattern?
To make optimal use of a large number of little items, this style suggests that objects be shared rather than continually manufactured. It’s one of the most basic structural patterns there is.
Consider the following scenario: you’re working on a football game. Consider building a class called football player that contains all players from all teams, as well as one object for each. A maximum number of players are actively employed at the same time in a game with 250 thousand participants, thus we have 250 thousand football player objects, but we only use 20–30 of them actively. Isn’t it enough merely to utilize the player objects actively in this case? In other words, we can turn 40 football players into the object of each individual football player using detailed information about each player. In other words, the players we see on the screen can be transformed and used by other teams in a different battle.
Consider a book: rather than constructing all 300 pages, we may generate just one page object and manage the content of the pages that will be displayed. This will not work for you if you wish to see all of the pages of a book at the same time, but how did this happen?
The most fundamental component of this pattern is memory management, which seeks to prevent us from holding objects in memory needlessly and continuously resetting the ones that aren’t being utilized.
A flyweight is an item that may be utilized in several settings at the same time but acts differently in each one. To put it another way, it can learn about the values we give it and adjust its behavior accordingly.
A fundamental distinction must be made in this circumstance.
The indicator of being an internal or main state, that is, being a page or being a football player is an internal state.
Another situation is the external situation, the number of pages is values such as what the player’s name is.
An approach of changing what the object does might not make much sense. If we continue from this thought, it can be concluded that producing 1 object and using all new objects by changing its internal and external properties.
The internal and external situation is designed to model the application in the best conditions. Being a football player can be done as an external situation, but it increases the complexity. This use will require constant modification of the object.
How many objects of the Flyweigt class will need to use at the same time also determines the number of objects to be created at the initial stage. The fewer objects are processed, the less memory is used.
It is created with the help of a factory method. Created flyweight objects can be put into an object pool.
The internal state of the created objects, that is, their basic tasks, are given. These objects can then be used over and over again in different contexts. The external states of objects differ depending on the context.
Java uses string values in this structure, that is, when we create a string, it is thrown into the pool and when you want to create a new object with the same value, it returns the value in the pool.
The Flyweight pattern is used to reduce the number of objects and facilitate their management in situations where there is a large object requirement.
Ideal where there is more than one object doing similar work. The same object can be used by showing it in different ways according to different external conditions.
Flyweight objects are often kept permanently in memory. For this reason, the use of object pools is common in this principle.
But not every object in every object pool is flyweight, those that are used repeatedly with different contexts are named that way.
Flyweight objects are generated by the factory method. The prototype mold can also be used for its manufacture.