Friday, 22 November 2013

Flyweight Design pattern - Introduction

Master Software Architecture & Clean Code!

Subscribe to Ram N Java for core software design patterns, clean coding techniques, and comprehensive Java guides!

SUBSCRIBE NOW

Introduction to the Flyweight Pattern

The Flyweight Design Pattern is a structural design pattern primarily used to optimize memory usage by sharing common data across multiple objects instead of keeping identical data in every single instance.

When an application requires a large number of objects that share significant amounts of state, creating separate instances quickly consumes JVM heap space and impacts performance. The Flyweight pattern prevents this by reusing existing matching objects from a shared pool.

Understanding Intrinsic vs. Extrinsic State

The fundamental concept behind the Flyweight pattern is splitting an object's state into two distinct categories:

  • Intrinsic State: Constant, immutable data stored internally within the flyweight object. This state is context-independent and can be safely shared across different callers.
  • Extrinsic State: Variable, context-dependent data that changes based on where and when the object is used. The client maintains this state and passes it to the flyweight method during execution.

Core Structural Components

  • Flyweight Interface: Outlines the operations that flyweight instances perform, accepting extrinsic data as parameters.
  • Concrete Flyweight: Implements the interface and holds the intrinsic state.
  • Flyweight Factory: Manages the pool/cache of flyweights, returning existing instances or creating new ones on demand.
  • Client: Holds or computes extrinsic values and requests flyweights via the factory.

Watch More Related Design Pattern Tutorials


Click here to watch in Youtube : https://www.youtube.com/watch?v=lY3ilaxi-7o

Click the below Image to Enlarge
Flyweight Design pattern - Introduction

















See also:

  • Flyweight Design pattern - Real Time Example
  • Flyweight Design pattern - Class Diagram
  • Flyweight Design pattern - Sequence Diagram
  • Flyweight Design pattern - Implementation
  • Flyweight Design pattern - Key Points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials