Structural

Flyweight

Description

Lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object.

Where to use

  • First-person shooters (particles).
  • \n
  • Text Editors (characters).

Real World Example

A Text Editor. Instead of storing font data for every letter 'A', it stores one 'Font object' for 'A' and 1000 pointers to it.

Code Example

class Flyweight:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Flyweight')