Using Flyweight pattern in Java project

Опубликовано: 09 Май 2025
на канале: Improve Your Programming skills
103
3

The flyweight pattern (structural design pattern) is used when we need to create many similar objects. Using Flyweight pattern, we reduce the creation of multiple objects. It help save memory and improve performance

If we build a game that creates 1000 soldiers. There are two types of soldier: Sniper and Commando. When using Flyweight pattern, we create 2 objects only. No need to create 1000 objects

Implementation steps:
1. Create an abstract class Soldier
2. Create CommandoSoldier class that extends Soldier
3. Create SniperSoldier class that extends Soldier
4. Create SoldierFactory