Flyweight Design Pattern

Hikmet Çakır
2 min readSep 3, 2023

--

Photo by Aldric RIVAT on Unsplash

Flyweight is a structural design pattern like Adapter, Composite, and Observer. This design pattern provides the minimization of creating similar objects and reduces memory usage instead of creating too many similar objects.

How To Implement

  1. Divide fields of a class that will become a flyweight into two parts:
  • The intrinsic state: the fields that contain unchanging data duplicated across many objects
  • The extrinsic state: the fields that contain contextual data unique to each object

2. The extrinsic fields should be sent as parameters to the relevant method.

3. A factory class must be created to create an object.

Example Implementation

Let’s suppose that we have a combat game that has two character types (Magician, Warrior) and the characters are fighting in the different locations of the map.

The character has a weapon and location. Additionally, I added some code pieces to the constructor of the Character class because of simulating problem. As I said before, the flyweight design pattern is used to prevent the creation of too many similar objects. If the character is created again and again, it will take a long time.

Basically, every magician and warrior has attack skill in the given position with the given weapon.

Characters can be created with the CharacterFactory class by profession type. Additionally, preventing similar object creation is being provided here.

CLI Output:

Magician is attacking with wand in Location(9,121)
Warrior is attacking with sword in Location(137,243)
Magician is attacking with wand in Location(109,381)
Warrior is attacking with sword in Location(420,23)
Magician is attacking with wand in Location(35,210)
Warrior is attacking with sword in Location(290,298)
Magician is attacking with wand in Location(444,127)
Warrior is attacking with sword in Location(480,1)
Magician is attacking with wand in Location(148,0)
Warrior is attacking with sword in Location(93,132)
Magician is attacking with wand in Location(481,431)
Warrior is attacking with sword in Location(278,75)
Magician is attacking with wand in Location(43,128)
Warrior is attacking with sword in Location(456,316)
Magician is attacking with wand in Location(462,174)
Warrior is attacking with sword in Location(434,494)
Magician is attacking with wand in Location(442,338)
Warrior is attacking with sword in Location(298,147)
Magician is attacking with wand in Location(219,206)
Warrior is attacking with sword in Location(394,47)
============Result============
Time taken: 4035 milliseconds
Time taken: 4.035 seconds

I hope everything’s clear for you. If you don’t understand any part, feel free to ask me. Also, I shared the codes I explained and more on GitHub. Additionally, I recommend that you practice with these structures. I coded these structures and these structures’ test scenarios. You can check out to repository.

I used various resources to prepare this essay. I indicated in the following. You can check it out.

--

--

No responses yet