Flyweight in Python Design Patterns

27 de novembro de 2026 Off Por Paulo Gonçalves Souza

Flyweight in Python Design Patterns

The flyweight interface declares common methods that will be used by concrete flyweight objects. For already loaded images, a flyweight object is created, which has some unique data like position within the page, but everything else is referenced to the cached one. As you can see in the following image, we created and stored one circle object in the Cache. My personal recommendation is to use the latter approach, as it makes it easier for applications to control the caching and lifetime of the flyweight objects (and, for example, to bound the size of the cache or to instantiate objects directly, bypassing the cache, when appropriate — such as in test code). In some cases, the flyweight construction is hidden from view (that is, you explicitly allocate an object that, under the hood, recycles a helper class), whereas in other cases it is explicit (e.g. there is a distinct class that is responsible for the lifecycle management of the flyweight objects that is separate from the flyweight object, itself).

Both flyweight classes implement the flyweight interface, which simply has a method called display to show information. As mentioned earlier, the interfaces simply specify the attributes that the plain objects passed as arguments must contain. If we observe, we have the four attributes that make up a Player, and through the constructor, we receive the parameters that allow us to internally store each attribute of this object. Internally, the pattern will create this object since it does not exist and store it in the data structure. The next method in the class, listFlyweights, simply iterates over the data structure where we have all the objects and displays the objects contained within it. As we can see in the first control block of the code, the if statement checks whether the data structure contains the object we are looking for, and if it does, we simply return it.

Now that we have categorized the internal states of FlyweightMidgetCar into intrinsic and extrinsic data, we can make it a flyweight object. This method defines the position of each FlyweightMidgetCar object at a given time, and hence cannot be shared. This means, these values can be shared between various FlyweightMidgetCar objects, and so we consider them as intrinsic data. The specific implementation is not that important, as it will vary depending on the programming language used.

As you can see, this class implements the IShape interface and provides the implementation for the Draw() method. One time, the color is red; another time, the color may be black. Now, if you read the definition, you will understand the flyweight design pattern very clearly. So, we need to store this circle object in the memory, i.e., in the cache. Instead of Flyweight Design Pattern creating new circle objects every time and filling them with green color, you can get the circle object from the Cache and fill it with green color.

Properly analyzing the problem domain and understanding the pattern can help in its successful implementation. Using the Flyweight pattern can offer significant memory savings, especially in scenarios where programs need to manage a large number of objects that share considerable amounts of information. The intrinsic state might be the base drink like “latte” or “cappuccino,” while the extrinsic state can be the customizations like “extra shot” or “almond milk.” Instead of creating a unique object for every possible combination, the coffee shop can use a base drink (intrinsic) and apply customizations (extrinsic) as needed. The flyweight pattern applies to a program using a huge number of objects that have part of their internal state in common where the other part of state can vary. If there is no object of the required type, the factory creates a flyweight of the requested type, adds it to the pool, and returns a reference to the flyweight.

Here, we created one dictionary collection object that will act as a Cache for our application. So, we created one method, i.e., SetColor, to set the color of a circle object at runtime. The circle has four attributes, i.e., Color, X Coordinate, Y Coordinate, and Radius.

Again, we need a way to uniquely identify our IPublication (flyweight) objects, so we use the same three-value tuple as the key, then immediately create a new IPublication instance variable. As briefly mentioned in the introduction, the overall purpose of the flyweight pattern is to make it easier to reuse objects whenever possible, ideally saving both memory and processor time. Even though I used movies as a real world example of the flyweight design pattern, we’re going to use more literary examples in our code sample.

There are several implementations for this pattern. This problem can be solved using the flyweight pattern. The flyweight object is returned from the cache for successive requests instead of being created again.