Flyweight Pattern - Lightweight Mode (Flyweight Pattern)

This article introduces the Flyweight Pattern using examples and scenarios from refactoring.guru.

Source: https://refactoring.guru/design-patterns/flyweight


Scenario:

You’ve developed an FPS game featuring missiles, bullets, and various cool effects, which you and your friends have started to play.

But, However, “It works on your machine”

On your friend’s computer, the game crashes after a few minutes! The gaming experience on your friend’s system is terrible. After debugging for several hours and sifting through the logs, you pinpoint the issue to RAM insufficiency. Your computer has superior specs, but your friend’s doesn’t, leading to RAM depletion.

The RAM consumption issues stem from the particle system you developed. Each particle, like bullets or missiles, spawns a particle object whenever it appears on the screen, eventually crashing the system due to RAM overload.

As shown in the image below, each particle takes up 21 KB. With 1,000,000 particles, that would require 21 GB of RAM.

from refactoring.guru

Here’s a scene from Overwatch.

How can the Flyweight pattern improve your game’s performance?


How Flyweight Reduces System Load

Revisiting the “Particle” class, consider the following:

You’ll notice two properties that can be made immutable (internal data):

  • color
  • sprite

Other attributes that vary (external data) include:

  • particle
  • coordinates
  • vector
  • speed

Looking back at the system:

The most resource-intensive element remains the sprite. With the same number of particles and without initializing new internal data, the requirement is 32MB of RAM. In contrast to the non-design pattern scenario requiring 21 GB, this design allows the game to run smoothly even on your friend’s computer.


Is it over? Have we fully utilized the design pattern?

Design patterns can be layered!


Adding an External Object Pool for Further Optimization

In practice, an “object pool” is often added to store the “internal information.”

In this case, since there is a main class, Game, that stores Particles, the system first checks the pool for unused particles to reuse them by injecting “external information” (vector, speed, coordinates). If no suitable particles are in the pool, a new one is initialized.

Once a particle is no longer needed, it is returned to the pool for future use.

This can be seen as a combination of the Object Pool and Flyweight patterns. In UIKit, an essential component uses this hybrid approach.

Further optimizations are made even to the external information using another layer of the Flyweight pattern.

Flyweight of a Flyweight. There’s always room for finer detail.


Further into your career, you might not start in a “real” game company in Taiwan; you could be an iOS frontend developer. No matter your field, everyone starts with frameworks in 2022. If you are an iOS frontend developer, consider which system components in iOS might be applying the “Flyweight Pattern (Flyweight Pattern).”

By Marvin Lin on August 10, 2022.

Canonical link

Updated: