FLYWEIGHT Pattern
Objective: FLYWEIGHT Pattern.
Flyweight design pattern, is the design pattern which share common attribute objects so when create new object it enhance performance and minimize memory usage, especially when we need to create thousands of units of same type in our games.
To start this I will create script and attach it to the enemy object , then I will turn this as prefab.
Before Implement Flyweight
Lets look at our enemy code for now.
If we create more object of enemy type, this code will be duplicate again for the same amount. This will require more memory usage since each object instantiated means new unique object is created.
So Imagine if 10000 unit created it will be chaos because it will hinder performance for our games especially for low end device.
Implementing Flyweight pattern.
To Implement this first we need to look to our enemy script and see what are trait which is sharable among all enemy.
For this script we can see that every enemy created will have the same speed and same maxHealth.
Solution.
To avoid a duplicate of this attribute we need to share this variables to every object created later time.
We do this by adding static type to our variable to make sure when we Instantiate this object the common attribute will not be duplicate/created again.
Note: There are other type can be used to create flyweight pattern.
See you in to the next one.