Using the flyweight pattern
The flyweight design pattern is mostly used when we want to reduce the amount of memory that is used to create the objects. This pattern is often used when we want to create something hundreds or thousands of times. Games with a forest structure often use this design pattern. This design pattern falls under the structural design category. In this pattern, the object, let's say the tree object, is divided into two parts, one that is dependent on the state of the object and one that is independent. The independent part is stored in the flyweight object, whereas the dependent part is handled by the client and sent to the flyweight object as and when invoked.
Getting ready
For this recipe, you will need a Windows machine with a working copy of Visual Studio.
How to do it…
In this recipe, we will find out how easy it is to implement the flyweight pattern:
Open Visual Studio.
Create a new C++ project.
Select a Win32 console application.
Add a source file called
Source.cpp
.Add...