Flyweight Design Pattern Explained With Simple Example: Structural Design Pattern Category - Just another WordPress site

Flyweight Design Pattern Explained With Simple Example: Structural Design Pattern Category

Flyweight Design Pattern Explained With Simple Example: Structural Design Pattern Category

It is especially helpful when working with several items that may be combined into shared flyweight objects to lower the total memory footprint. The Flyweight Factory manages the pool of Flyweight objects. The intrinsic data is stored in a central place and reused, while the extrinsic data is kept separately. Extrinsic state is stored or computed by client objects, and passed to the Flyweight when its operations are invoked. Intrinsic state is stored (shared) in the Flyweight object. In this example, the shape of the angry bird is constant and stored in the memory, so the shape of the angry bird is in an Intrinsic state.

We then stored the object in the HashMap, and finally returned the object to the client. In the CarFactory class, we created a HashMap to store flyweight (RaceCar) objects and written a getRaceCar() method- a factory method that returns a flyweight object. NOTE – Notice how the moveCar() method does not store state in the flyweight object. In both the classes above, we maintained a counter through their constructor that will later enable us to check the number of flyweight objects created.

The Flyweight pattern is an excellent solution when dealing with systems that need to create and manage a large number of similar objects, which can result in high memory consumption if not managed properly. Intrinsic state is stored in the flyweight; it consists of information that is independent of the flyweight’s context, allowing it to be shared. On the other hand, flyweight objects cannot make assumptions about the context in which they operate. The intrinsic state represents properties that are common among multiple objects, while the extrinsic state represents properties that are unique to each object.

Now, Let’s look at the final product definition which will share the common information via shared object which it will get from Flyweight factory whereas unique information will be stored in this class only. We end the example with an output from library.GetPublicationCount, which simply retrieves the quantity of IPublication objects stored in the library. In video games, it is usual that you have to display the same sprite (i.e. an image of an item of the game) several times. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored internally. A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a word processor. The factory class will have an internal cache that stores the first created flyweight object.

The key the document at the source idea behind the Flyweight pattern is to separate the intrinsic state and extrinsic state. Master Software Architecture & Design, create real-world projects, and earn your certificate. Here you will learn SOLID principles, Gang-of-Four patterns with real-world examples in .NET/C#.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. He is a renowned Speaker, Solution Architect, Mentor, and 11-time Microsoft MVP (2016–2026). If dynamic attributes are frequently changing, managing the extrinsic state separately can become complex, reducing the effectiveness of the pattern.

And as it is stored in the memory and as it is constant, it belongs to the Intrinsic State. On the other hand, Extrinsic states are things that are not constant and need to be calculated on the Fly; hence, they can not be stored in memory. Here, the circle object we stored in the cache has no color.

The Flyweight Factory uses a modern Map and Optional to manage book caching. Using record for BookImpl is perfect since the intrinsic state fields (title, author, isbn) are immutable. Here is a refined implementation of the Flyweight pattern for the same Library System using modern Java features up to JDK 21. In this way, it optimizes memory usage by reusing an intrinsic data among multiple objects and passing the extrinsic data on demand. In the Flyweight pattern, the concepts of intrinsic state and extrinsic state are key to optimizing resource usage.

The next class is SportsSimulator, which is responsible for storing the data structure. Additionally, we include a toString method, which we have renamed as displayInfo, that simply shows the information of the object. Let’s proceed to see the implementation of this first solution, which does NOT use the Flyweight pattern, to detect where the resource leak is that we could improve with the pattern.