
Episode 12 · Season 5
Software Design - The Flyweight Pattern
About this episode
We continue a look at the structural patterns with a stop at the flyweight. This pattern is one I find ignored more often than it should be. In particular, the large number of data items we typically model in modern applications are often best handled through this pattern.
The Flyweight Pattern Defined
As always, we will start with the "Gang of Four" intent to set the stage for our discussion.
"Use sharing to support large numbers of fine-grained objects efficiently."
Short and sweet. This intent is to model large collections in the best way based on data needed and memory considerations. A flyweight is an object-oriented approach to data modeling that is practically an index. We only keep up with the data we need for each element. This approach reduces the overhead of moving around fully defined objects.
Applying The Pattern
The flyweight implementation is a smallish class. The typical approach is for a flyweight to be based on a larger class. Then it simply ignores many of the attributes. It often will have the bare minimum of attributes required to implement common interfaces. It might even provide a method to return a complete, and populated instance.
If this sounds familiar, then that is because the flyweight pattern is often used to implement lazy loading. The difference in lazy loading is that those objects sometimes have all of the class properties. However, they do not populate the values immediately. A true use of this pattern does not carry that overhead. This factor is critical when you consider the possibility of thousands, millions, or more instances of a class. At that point, every property, every byte counts.
Java, PHP, C#, etc.
As mentioned before, a flyweight is just an approach to a class. Thus, there is no need for a language to support any special object-oriented features. Even inheritance is not a requirement for a flyweight. When it is implemented it just needs to be able to handle requests that can be made on a full object. This might even include going out to populate values that are not populated or retrieve a property value temporarily.
In the end, the flyweight pattern is one you should keep in mind in situations where a large number of instances may occur at any one time. Common examples of this include classes that model rows in a database, images in a library or even points on a map.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…