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
26
votes
4 answers

What is the reason for the name of the Flyweight Design Pattern?

I'm a non native English speaker, and trying to grasp a better understanding of that design pattern, I'm interested in the origin of that word for naming a pattern. What are the motivations for that name? As far I know the flyweight design pattern…
Alex. S.
  • 143,260
  • 19
  • 55
  • 62
24
votes
3 answers

Flyweight vs object pool patterns: When is each useful?

As far as I know the object pool is a creational pattern and the flyweight is a structural pattern, but actually I can´t see very much difference between the two. Could someone please explain to me the difference and when each could be useful in an…
edgar9000
  • 241
  • 2
  • 3
21
votes
7 answers

How does java implement flyweight pattern for string under the hood?

If you have two instances of a String, and they are equal, in Java they will share the same memory. How is this implemented under the hood? EDIT: My application uses a large number of String objects, many of which are identical. What is the best way…
Dan
  • 11,077
  • 20
  • 84
  • 119
18
votes
4 answers

Understanding the Flyweight pattern

Intent: The intent of this pattern is to use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary. Objects can share state via static fields. What is the…
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
16
votes
3 answers

Prototype Vs. Flyweight Design Patterns

I need to find out some differences between Prototype D.P and Flyweight D.P. I know that the basic difference is that the former makes deep copy. Whereas the letter makes shared object. My lecturer said that there are more differences. Someone know…
Matan
  • 281
  • 3
  • 7
14
votes
6 answers

Is Java's String Intern a flyweight?

Does the implementation of Java's String memory pool follows flyweight pattern? Why I have this doubt is, I see that there is no extrinsic state involved in Intern. In GoF I read that there should be a right balance between intrinsic and extrinsic…
Joseph
  • 877
  • 8
  • 20
11
votes
3 answers

Flyweight and Factory problem with IDisposable

I seem to be mentally stuck in a Flyweight pattern dilemma. First, let's say I have a disposable type DisposableFiddle and a factory FiddleFactory: public interface DisposableFiddle : IDisposable { // Implements IDisposable } public class…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
11
votes
4 answers

What is the difference between intrinsic and extrinsic state as described in Flyweight Pattern?

From the chapter on FlyWeight Pattern inside Gang of Four the FlyWeight pattern is applicable when most object state can be made extrinsic. What does extrinsic state mean ? I get the feeling that this pattern is used for sharing of objects . If…
Geek
  • 26,489
  • 43
  • 149
  • 227
10
votes
2 answers

How to make boost unordered_map to support flyweight

I am trying to do the following: boost::unordered_map, boost::flyweight > map; boost::flyweight foo(name); map[foo] = foo; But the compiler complains: "error C2665:…
Fredrik
  • 893
  • 2
  • 10
  • 20
10
votes
5 answers

The best alternative for String flyweight implementation in Java

My application is multithreaded with intensive String processing. We are experiencing excessive memory consumption and profiling has demonstrated that this is due to String data. I think that memory consumption would benefit greatly from using some…
Dan
  • 11,077
  • 20
  • 84
  • 119
9
votes
2 answers

Why is the (GoF) Flyweight a structural (and not a creational) design pattern?

To my understanding, the flyweight design pattern is not so different from the factory or singleton design patterns. It is just a factory that produces immutable (and pooled) objects. It is just a singleton that provides one instance per type (of…
Nicola Ferraro
  • 4,051
  • 5
  • 28
  • 60
9
votes
2 answers

Flyweight pattern vs static fields

In my understanding the purpose of the flyweight pattern is to decrease memory footprint and increase performance by sharing common, extrinsic state. Why would anyone prefer to implement the pattern over storing the shared state in static…
Tamas Pataky
  • 353
  • 4
  • 16
8
votes
1 answer

How to implement flyweight pattern in php?

This is its definition: Use sharing to support large numbers of fine-grained objects efficiently. But I can't figure out what it means exactly. Can you elaborate with a tiny demo?
user198729
  • 61,774
  • 108
  • 250
  • 348
8
votes
3 answers

What is the difference between Builder Pattern and Flyweight Pattern?

What is the difference between Builder Pattern and Flyweight Pattern in terms of usage, as both of them deals with large number of objects?
user366312
  • 16,949
  • 65
  • 235
  • 452
8
votes
3 answers

Python copy-on-write behavior

I'm working on a problem where I'm instantiating many instances of an object. Most of the time the instantiated objects are identical. To reduce memory overhead, I'd like to have all the identical objects point to the same address. When I modify…
DMack
  • 1,117
  • 1
  • 10
  • 14
1
2 3 4 5 6 7