Flyweight Pattern (English: Flyweight Pattern) is a software design pattern. It uses shared objects to minimize memory usage and share information with as many similar objects as possible; it is suitable for large numbers of objects that simply use an unacceptably large amount of memory due to duplication. Usually part of the state in an object can be shared. A common practice is to put them in external data structures and pass them to Flyweight when needed.
If you use a large number of a certain integer in a program, such as 1, assuming that this integer object occupies 100kB of memory, and you use it 1 million times, then the memory occupied by these 1 is still 100kB.
If it is C language on a 32-bit machine, an integer is 4byte, and the memory size occupied by 1 million integers is 4B * 1000k, which is 4MB.
How to put it this way, this feature cannot be said to be a benefit or a disadvantage. These two modes have their own advantages and disadvantages in different scenarios.
Save memory. There is no need to make a new copy when passing between variables or between actual and formal parameters. It’s crazy to think that when writing C++, you may copy a large vector without paying attention.
Flyweight Pattern (English: Flyweight Pattern) is a software design pattern. It uses shared objects to minimize memory usage and share information with as many similar objects as possible; it is suitable for large numbers of objects that simply use an unacceptably large amount of memory due to duplication. Usually part of the state in an object can be shared. A common practice is to put them in external data structures and pass them to Flyweight when needed.
If you use a large number of a certain integer in a program, such as
1
, assuming that this integer object occupies 100kB of memory, and you use it 1 million times, then the memory occupied by these1
is still 100kB.If it is C language on a 32-bit machine, an integer is 4byte, and the memory size occupied by 1 million integers is 4B * 1000k, which is 4MB.
How to put it this way, this feature cannot be said to be a benefit or a disadvantage. These two modes have their own advantages and disadvantages in different scenarios.
Save memory. There is no need to make a new copy when passing between variables or between actual and formal parameters. It’s crazy to think that when writing C++, you may copy a large vector without paying attention.