The object pooling pattern
The object pooling design pattern is a type of creational or abstract factory design pattern that uses a stack to hold a collection of initialized object instances. It is excellent for use in situations when you will have either a large number of objects that need to be spawned or objects that will be created and destroyed rapidly.
Since we will be shooting projectile objects from a weapon – which can be performed at a high rate by the player – this is a great place to apply object pooling because repeatedly instantiating and destroying objects comes with a high cost. In this case, object pooling provides a way to optimize CPU, memory, and garbage collection (GC).
Rather than creating a new projectile object directly every time the player needs to shoot, we’ll instead reuse an already instantiated projectile object by requesting it from the objects in the pool. As such, the Object Pool provides methods for requesting (getting) and...