Questions tagged [flyweight-pattern]

Flyweight is a design pattern that minimizes an object's memory use by sharing as much of its data as possible with other similar objects. It is one of the Gang of Four's structural design patterns. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

Flyweight is a design pattern that minimizes an object's memory use by sharing as much of its data as possible with other similar objects. It is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the Flyweight objects temporarily when they are used.

A classic example usage of the Flyweight pattern is the data structures for graphical representation of characters in a word processor. It might be desirable to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a Fyweight 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.

Flyweight is one of the Gang of Four's structural , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

See Wikipedia for more.

91 questions
1
vote
2 answers

How flyweight design pattern maintains different objects?

(i found this example while reading the flyweight ) let's assume there is an object called soldier in a game and this object differs by it's location only now my question is if i'm to use the flyweight pattern on this object , i may ask to obtain…
1
vote
1 answer

What happens to the extrinsic state when using the Flyweight Design Pattern?

I am currently researching software design patterns and have been somewhat perplexed with the flyweight design pattern's implementation. As far as I understand, the pattern works by separating the shared/constant intrinsic data from the unique,…
Squiggle
  • 13
  • 4
1
vote
1 answer

Appropriate datastructure for flyweight

I am trying to apply the flyweight pattern in a program that generates clouds. I have a class that represents intrinsic states of clouds. A cloud type is defined by its attributes : class CloudType { float size; float altitude; String…
1
vote
1 answer

Flyweight design pattern getting Object parameter as its key?

I am trying to reduce duplicates of Book objects using Flyweight design pattern but got stuck at some point. For example, assuming that there is a book class which contains some variables such as bookName, published_date and Langauge, and someone…
1
vote
1 answer

Is the Flyweight design pattern applicable for reducing instantiation costs?

The Design patterns book gives the following applicability for the Flyweight design pattern (bold emphasis mine): Applicabilty The Flyweight pattern’s effectiveness depends heavily on how and where it’s used. Apply the Flyweight pattern when all…
1
vote
1 answer

generic flyweight pattern in Swift

I'm trying to implement a generic flyweight pattern in Swift. In my code, I'm using a dictionary of weak references to weak references. The base protocol (Node) just has a position. Changing the position creates a new Node. The implementations I've…
Taylor
  • 5,871
  • 2
  • 30
  • 64
1
vote
2 answers

Java Intern pool implementation creates too many temporary objects

I have made an intern pool in java using the same idea as string intern. Simply put, I maintain a WeakHashMap Every time when the map contains the object, it will return the same object, the benefit is it will save java heap memory. For…
Yiwei
  • 89
  • 8
1
vote
2 answers

Flyweight pattern objects are immutable?

We know , Java intern string pool is based on Flyweight design pattern. Also the String object is immutable. Is it compulsary that all the objects which are using Flyweight pattern is Immutable? . What is the difference between mutable and immutable…
JavaUser
  • 25,542
  • 46
  • 113
  • 139
1
vote
2 answers

Error when using staticmethod with flyweight decorator

I have a class for making sprites flyweight and I am using a decorator to call this class. Here is some code: class flyweight: def __init__(self, cls): self._cls = cls self.__instances = dict() def __call__(self, title): …
1
vote
2 answers

Flyweight pattern - What is the point of an unshared concrete instance?

As we all know, in the UML diagram of the Flyweight Pattern there is an unshared concrete instance, and it implements the interface flyweight. My question is, why should it implement it if its extrinsic state is kind of pointless? I mean, for the…
john
  • 41
  • 1
  • 5
1
vote
0 answers

JavaScript + Node.js. Flyweight pattern query

While working my way through one of the Design Patterns courses that Pluralsight offer, it touched on the Flyweight Pattern, with an example in node. The examples in the course contrasted not using flyweights at all with using a flyweight made up of…
parsfan82
  • 11
  • 2
1
vote
1 answer

How to reuse GUI objects (Flyweight pattern)

In my Java app i'm having a complex GUI which has certain JPanel (custom control) repeated, lets say, 100 times (in reality it could be more, but i don't wanna make this example too complex). Each of those 100 JPanels contains 4 JTextBox controls…
guest86
  • 2,894
  • 8
  • 49
  • 72
1
vote
1 answer

Using boost::flyweight inside struct T {} (ie, recursive flyweights)

I'm trying to define an immutable file-path value type, taking advantage of boost::flyweight to share path components. Something like this: struct filepath_data; typedef boost::flyweight filepath; struct filepath_data { …
John Bartholomew
  • 6,428
  • 1
  • 30
  • 39
1
vote
1 answer

visualvm retained size same after using flyweight pattern

I have an odd situation. I have a simple flyweight factory that allows me to reuse instances that are equal() in a graph of objects. When I serialize the root object, with and without use flyweight, to measure its benefit, I go from 2,014,169 bytes…
drrob
  • 632
  • 7
  • 16
1
vote
2 answers

I don't see any use of flyweight pattern. Is it really useful?

To apply flyweight pattern, we need to divide Object property into intrinsic and extrinsic properties. Intrinsic properties make the Object unique whereas extrinsic properties are set by client code and used to perform different operations. But my…
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32