Member-only story

FlyWeight design pattern

It is one of the structural design patterns.

This pattern helps to reduce the memory usage by sharing data among multiple objects.

Important question ask top of this design patter: Design word processor, text editor and design game.

Let's see the problem first:-

Suppose we have class Robot with some property coordinateX, coordinateY, type(robotic type), body (makes object costly because its create 2D image and consume exp 32 KB memory).

Now we have a requirement to create 1k human robotics and 1k dog robotics.

so we have for loop and create object and cause memory issue.

Observe and understand, when to use this pattern.

  1. When memory is limited.
  2. When objects shared data: like in the ss type and data(humanoidSprite) are share among 1 k object are same.
    - Intrinsic data: shared among objects and remains same once defined one value(means if we create 1k robot, once define value for one then remains same for all). like: Type and body is intrinsic data
    - Extrinsic data: change based on client input and differs from one object to another. like: X and Y axys are Extrinsic.
  3. Creation of object is expensive.

--

--

No responses yet