1. What is Flyweight Pattern
Flyweight Pattern is mainly used to reduce the number of objects created to reduce Memory usage and improved performance. This type of design pattern is a structural pattern, which provides a way to reduce the number of objects to improve the object structure required by the application.
Flyweight mode tries to reuse existing objects of the same kind. If no matching object is found, a new object is created. We will demonstrate this mode by creating 5 objects to draw 20 circles distributed in different positions. Since there are only 5 available colors, the color attribute is used to check the existing Circle object.
2. Advantages and disadvantages
Advantages: Greatly reduce the creation of objects, reduce system memory, and improve efficiency.
Disadvantages: It increases the complexity of the system, and needs to separate the external state and the internal state, and the external state is inherent in nature and should not change with the change of the internal state. Change, otherwise it will cause chaos in the system.
3. Application scenarios
1, String in JAVA, if there is, return, if not, create A string is stored in the string buffer pool. 2. data pool of the database (connection pool)
4 .UML class diagram