看了下,解释说这是「享元模式」,还是不太理解,求问这样有什么好处,毕竟这样基于对象的要占用好多内存的。
阿神2017-04-17 13:47:01
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.
迷茫2017-04-17 13:47:01
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.
大家讲道理2017-04-17 13:47:01
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.