Summer Sale is Live! Unlock 40% OFF on All Job-Oriented Training Programs – Limited Time Only! Offer Ending in
D
H
M
S
Get Now

Structural Design Pattern: Flyweight Design pattern

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:01:00

Define Flyweight Design Pattern

The flyweight pattern is used when there is a need to construct a huge number of objects with essentially identical properties and some common data. A few shared objects can replace many non-shared ones.

Concepts of Flyweight Design Pattern

The flyweight pattern makes use of both intrinsic and extrinsic data.

  • Intrinsic Data: The shared flyweight objects' characteristics store intrinsic data. This information is stateless and normally unchanging; if something changes, it will be reflected in all objects that reference the flyweight.
  • Extrinsic Data: Extrinsic data is computed during runtime and stored outside of a flyweight object. As a result, it has the potential to be meaningful.

Examples of Flyweight Design Patterns

Examples of flyweight patterns are:

  • Game Objects
  • Social Media Posts
  • List of Users

Flyweight Design Pattern with UML diagram

The classes, interfaces, and objects in the above UML class diagram are listed below:

  • Flyweight: This interface defines the properties of flyweight objects.
  • ConcreteFlyweight: This class inherits from the Flyweight class.
  • UnsharedFlyweight: This class inherits from the Flyweight class and allows for information sharing; instances of concrete flyweight classes can be created that are not shared.
  • FlyweightFactory: This class manages a set of pre-built flyweight objects. When the client invokes the GetFlyweight method, it determines whether the requested flyweight object exists in the collection. If it's found, it's returned; otherwise, a new object is generated, added to the collection, and returned.

When to use a Flyweight Design Pattern?

  • The flyweight design pattern is excellent for creating a huge number of similar products with high storage costs.
  • A few shared objects can replace many non-shared ones.
  • The majority of the state can be stored on disc or calculated during runtime.
Self-paced Membership
  • 24+ Video Courses
  • 850+ Hands-On Labs
  • 500+ Quick Notes
  • 225+ Skill Tests
  • 120+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this