The Flyweight Design Pattern in Python

Fhuseynov
2 min readOct 10, 2023

--

What is the Flyweight Design Pattern?

The Flyweight pattern is all about efficiency. It’s used when your application needs to create a large number of similar objects, and you want to save memory by sharing as much common state as possible among these objects. This pattern promotes reusability and reduces the overall memory footprint.

The key idea behind the Flyweight pattern is to separate an object’s intrinsic and extrinsic state.

  • Intrinsic state: Represents the shared, immutable parts of an object.
  • Extrinsic state: Represents the context-specific, mutable parts of an object.

By isolating the intrinsic state and sharing it among multiple objects, you can significantly reduce memory consumption. The extrinsic state, which is unique to each instance, can be managed independently.

Implementing the Flyweight Pattern in Python

Let’s explore the Flyweight pattern through an example. Suppose we are building a simple text editor, and we want to represent characters as objects. However, instead of creating a separate object for each character, we’ll use the Flyweight pattern to share the common character information.

Here’s a Python implementation of the Flyweight pattern for our text editor:

class CharFlyweight:
def __init__(self, char):
self.char = char

class CharFactory:
char_flyweights = {}

@staticmethod
def get_char(char):
if char not in CharFactory.char_flyweights:
CharFactory.char_flyweights[char] = CharFlyweight(char)
return CharFactory.char_flyweights[char]

class Character:
def __init__(self, char, font_size):
self.char_flyweight = CharFactory.get_char(char)
self.font_size = font_size

def render(self):
print(f"Character: {self.char_flyweight.char}, Font Size: {self.font_size}")

# Client code
characters = []
characters.append(Character('A', 12))
characters.append(Character('B', 14))
characters.append(Character('A', 12)) # Reusing 'A' from flyweight

for character in characters:
character.render()

In this example, we have three main classes:

  1. CharFlyweight: Represents the intrinsic state of a character (the character itself).
  2. CharFactory: Maintains a pool of flyweight objects and ensures that each unique character is created only once.
  3. Character: Represents the extrinsic state of a character, including font size.

In the client code, we create character objects. When we create a character, the CharFactory checks if a flyweight for that character already exists. If it does, it returns the existing flyweight; otherwise, it creates a new one. This ensures that we reuse the same character flyweight for identical characters, reducing memory usage.

Conclusion

The Flyweight design pattern is a powerful tool for optimizing memory usage in your Python applications. By separating intrinsic and extrinsic state and sharing common elements among objects, you can efficiently manage a large number of similar objects without sacrificing performance or readability. When dealing with scenarios where memory optimization is critical, consider incorporating the Flyweight pattern into your design.

--

--