Flyweight pattern is primarily used to
- reduce the number of objects created
- decrease memory footprint
- increase performance.
Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.
This type of design pattern comes under structural pattern.
Below is an example with diagram and code as further explanation.
Diagram
Code Explanation.
We will demonstrate this pattern by drawing 20 circles of different locations but we will create only 5 objects.
Only 5 colors are available so color property is used to check already existing Circle objects.
原文地址:https://www.cnblogs.com/codingyangmao/p/11275399.html
时间: 2024-10-18 08:53:23