← Back to Home

Flyweight Pattern

Use sharing to support large numbers of fine-grained objects efficiently. Flyweight reduces memory usage by sharing common data between similar objects.

What is it?

The Flyweight pattern minimizes memory usage by sharing as much data as possible with similar objects. It’s especially useful when dealing with large sets of objects that share state.

Example

interface Tree {
  render(x: number, y: number): string;
}

class TreeType implements Tree {
  constructor(private name: string, private color: string, private texture: string) {}

  render(x: number, y: number): string {
    return `🌳 Drawing ${this.name} at (${x}, ${y}) with ${this.color} and ${this.texture}`;
  }
}

class TreeFactory {
  private static treeTypes: Map<string, TreeType> = new Map();

  static getTreeType(name: string, color: string, texture: string): TreeType {
    const key = `${name}_${color}_${texture}`;
    if (!this.treeTypes.has(key)) {
      this.treeTypes.set(key, new TreeType(name, color, texture));
    }
    return this.treeTypes.get(key)!;
  }
}

class TreeInstance {
  constructor(private x: number, private y: number, private type: TreeType) {}

  draw(): string {
    return this.type.render(this.x, this.y);
  }
}

const trees: TreeInstance[] = [];
for (let i = 0; i < 10000; i++) {
  const type = TreeFactory.getTreeType('Oak', 'Green', 'Rough');
  trees.push(new TreeInstance(i, i, type));
}

Common Uses

  • Rendering many UI elements (icons, trees, text characters)
  • Games with repeated entities
  • Data visualization tools

When to Use

  • You need to create a large number of similar objects
  • You want to reduce memory consumption

Caution

  • Can make code more complex
  • Flyweight objects should be immutable