quiz image

Design Patterns: Flyweight Pattern

SelfSufficientRadon avatar
SelfSufficientRadon
·
·
Download

Start Quiz

Study Flashcards

18 Questions

What is the main problem that the Flyweight pattern solves?

Redundant data storage in objects

Why did the game crash on the friend's computer?

The computer had less RAM than the developer's machine

What was the distinctive feature of the game?

Realistic particle system

What fields in the Particle class consume the most memory?

Color and sprite

What is the main difference between the color and sprite fields and the other fields in the Particle class?

The other fields are unique to each particle

Why did the developer choose to implement a realistic particle system?

To make the game more realistic and immersive

What was the consequence of not using the Flyweight pattern in the game?

The game crashed due to insufficient RAM

What is the benefit of using the Flyweight pattern in the game?

Reduced memory usage and ability to fit more objects in RAM

What represents the always changing context in which a particle exists?

Extrinsic state

Which of the following is a characteristic of the intrinsic state?

It remains constant for each particle

What is the main reason for applying the Flyweight pattern?

To reduce the memory consumption of objects

What is the purpose of a separate context class in the Flyweight pattern?

To store the extrinsic state of a particle

Why is immutability important in the Flyweight pattern?

To ensure that the flyweight object can be reused

What is the purpose of a flyweight factory?

To provide convenient access to various flyweight objects

Where can the factory method be placed in the Flyweight pattern?

In the flyweight class or a separate factory class

What is the advantage of using a separate context class in the Flyweight pattern?

It allows for more elegant and efficient storage of extrinsic state

What is the result of applying the Flyweight pattern in a game?

A thousand small contextual objects can reuse a single heavy flyweight object

What is the relationship between the intrinsic state and the extrinsic state?

The intrinsic state and the extrinsic state are separate and distinct

Study Notes

Flyweight Design Pattern

  • A structural design pattern that lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects.
  • Aimed at reducing memory consumption by minimizing duplicate data.

Problem with Particle System

  • Each particle (bullet, missile, or shrapnel) was represented by a separate object containing plenty of data.
  • Newly created particles no longer fit into the remaining RAM, causing the program to crash.
  • Problem emerged due to insufficient amount of RAM on less powerful computers.

Solution

  • Identify fields that consume a lot of memory (e.g., color and sprite) and store almost identical data across all particles.
  • Separate the object's state into intrinsic state (constant, unique to each particle) and extrinsic state (changing, unique to each particle).
  • Store the intrinsic state within the object (flyweight) and pass the extrinsic state to specific methods.

Flyweight Pattern

  • Stop storing the extrinsic state inside the object.
  • Only the intrinsic state stays within the object, letting you reuse it in different contexts.
  • Result: fewer objects needed, since they only differ in the intrinsic state.

Extrinsic State Storage

  • Move the extrinsic state to the container object (e.g., main Game object) or a separate context class.
  • Store coordinates, vectors, and speed of each individual particle, along with references to the flyweight object.
  • Use a single array in the container class or a separate context class for more elegance.

Flyweight and Immutability

  • Ensure the flyweight object's state can't be modified.
  • Initialize the state via constructor parameters, and don't expose setters or public fields.

Flyweight Factory

  • Create a factory method to manage a pool of existing flyweight objects.
  • The method accepts the intrinsic state of the desired flyweight, looks for an existing object, and returns it if found, or creates a new one.
  • Place the factory method in a flyweight container, a new factory class, or as a static method inside the flyweight class.

Understanding the Flyweight design pattern, which optimizes memory usage by sharing common state between multiple objects.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser