CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
        Adapter
        Bridge
        Composite
        Decorator
        Facade
        Flyweight
        Proxy
    Behavioral Patterns


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 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 it 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.
Structure

Flyweight Design Pattern

Participant classes
  • 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.
Example

Implementation Code

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



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 is from his courses on design patterns.







Yearly Subscription


Design Patterns in C#


Advanced JavaScript

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy