> Design Patterns > Flyweight Design Pattern

Flyweight Design Pattern

Flyweight design pattern is used to support sharing of objects when the objects are in large number with details to granularity of system.

Intent

  • Use sharing to support large numbers of fine-grained objects efficiently.

The intent of flyweight design pattern is to share the fine-grained objects to make the system efficient.

Problem

  • Having large number of objects to the granularity of system makes the system complex and inefficient.

It looks good to have objects to the granularity of system as it gives flexibility in design but having large number of objects may make the system complex and affect the efficiency.

Solution

  • Come up with an approach to share the objects at fine granularity to make the system simple and efficient.

We can have the shared object called flyweight which can be used in multiple context. So we will identify what information is independent for object which is related to the object context. The object will have independent information so that it can be shared. The context information will depend on object context as it varies and will be provided by client. The independent information is called intrinsic state and context information is called extrinsic state. The operation of flyweight object will be done based on these states.

Where flyweight design pattern is applicable?

  • There are large number of objects to the granularity of system.
  • There can be differentiation of intrinsic and extrinsic state.
  • The objects can be shared with their independent information.

Flyweight Design Pattern UML Structure

Flyweight Design Pattern UML Structure

Participant classes of flyweight design pattern

  • FlyweightFactory class manages the pool of Flyweight objects. If flyweight object already exists then it provides the instance from the pool, otherwise it creates new and add it to the pool.
  • Flyweight class provides the interface Operation with extrinsic state.
  • ConcreteFlyweight class implements the method Operation and has intrinsic state which is independent of its context.
  • UnsharedConcreteFlyweight is not shared and can have Flyweight object in their structure.

How they work together?

  • Client gets the Flyweight object through FlyweightFactory. FlyweightFactory manages the pool of flyweight objects, if its already existing then it provides the same, otherwise it creates new one, adds it to the pool and provides it to the client.
  • Client gets or computes the extrinsic state information. It provides the extrinsic state information while using interface Operation. ConcreteFlyweight does the operation based on intrinsic and extrinsic state.
  • So we can see the number of objects will be drastically reduced by sharing the flyweight objects. This will have reduction of storage, decrease in memory requirement and will obviously make the system efficient.

Flyweight Design Pattern Example

Here is the flyweight design pattern example.

UML diagram requires UML components - class, object, association, generalization, multiplicity etc. Share these objects for efficiency.

Implementation Code

C++ Flyweight Pattern in C++ Flyweight Pattern C++ Example
C# Flyweight Pattern in C# Flyweight Pattern C# Example
Java Flyweight Pattern in Java Flyweight Pattern Java Example
Python Flyweight Pattern in Python Flyweight Pattern Python Example
JavaScript Flyweight Pattern in JavaScript Flyweight Pattern JavaScript Example
PHP Flyweight Pattern in PHP Flyweight Pattern PHP Example
Ruby Flyweight Pattern in Ruby Flyweight Pattern Ruby Example
Swift Flyweight Pattern in Swift Flyweight Pattern Swift Example
Objective-C Flyweight Pattern in Objective-C Flyweight Pattern Objective-C Example


Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth, Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of multiple products. This article Flyweight Design Pattern is from his Design Patterns course.