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
7
votes
3 answers

Flyweights with Boost and external data sources

Maybe there is a simple way around this that I'm not seeing, so hopefully somebody can explain it to me. Let's say I have a class: class A { public: const double parameter; const std::string name; const std:: string fileName; A(const…
tpg2114
  • 14,112
  • 6
  • 42
  • 57
7
votes
5 answers

Flyweight Examples in Java

I am trying to create a flyweight object in Java. I've worked with a similar concept in Objective-C (Singleton Classes in Objective-C // I believe they are the same thing). I am trying to find a tutorial or an example or explanation online to learn…
darksky
  • 20,411
  • 61
  • 165
  • 254
7
votes
4 answers

Hibernate and Flyweight

Is there a way to use Flyweight objects with the hibernating persistence mapping? My data model contains many objects that will be the same. Instead of having a separate instance for each of those same objects I'd like to use the Flyweight Design…
paweloque
  • 18,466
  • 26
  • 80
  • 136
6
votes
6 answers

Is there any Java flyweight pattern implementation out there?

I've been looking for a flyweight pattern implementation and gave up after reaching page 20 of Google search. While there are countless stupid examples out there, it seems no one has ever published are reusable implementation in Java. For me,…
Sebastien Diot
  • 7,183
  • 6
  • 43
  • 85
6
votes
3 answers

What is the difference between Flyweight design pattern and Java cache

I read about Flyweight design pattern and got to know that it stores the objects that can be shared so as to save on heap usage. Java cache also saves the objects that can be reused later so as to save memory. Then what is the real difference…
Aakash Saxena
  • 113
  • 1
  • 7
5
votes
2 answers

Flyweight : Strings already use String pool : Does it makes sense to pool String objects for Flyweight?

Strings are already using Flyweight Design Pattern. Will it be beneficial/performant to pool common String objects. As the Strings will be already pulled from the String pool?
TheHat
  • 434
  • 3
  • 6
  • 12
5
votes
2 answers

Prototype vs Flyweight

I start to learn design-patterns. I understand that prototype is for making an exact copy of an object I already have and Flyweight is for making similar objects. I've programmed 2D platformer game like Mario (in Java). There are a lot of enemies…
5
votes
1 answer

multiton pattern vs flyweight pattern

I cannot see any single difference between Multiton and Flyweight patterns. Please explain what is the difference?
Narek
  • 38,779
  • 79
  • 233
  • 389
3
votes
2 answers

string to flyweights of string conversion: Better performance option

so i have a flyweight of strings type: typedef boost::flyweight< std::string, boost::flyweights::intermodule_holder > SymbolName_t; and i want to push a instance of this into a vector of them, but the naive approach won't work: void PushSome(…
lurscher
  • 25,930
  • 29
  • 122
  • 185
3
votes
2 answers

State and FlyWeight patterns

Can anybody share example of using state pattern with flyweight pattern (flyweight pattern is for creating state objects to save memory)? UPDATE: How to use a combination of state and fw patterns?
drifter
  • 683
  • 1
  • 11
  • 24
3
votes
1 answer

Can we take a const ref of a substring?

I have a class, which contains a string as a private member. The class has public member functions which all return a const ref to a std::wstring. All of the const-ref strings being returned are substrings of the private member. I could store each…
brandon_busby
  • 122
  • 11
3
votes
0 answers

Game State Tree - deep cloning not possible, need way to create multiple states without overwriting data

For a project in my AI class, we are making AIs for the game ingenious and are having trouble creating a game-state tree. Our tree is represented by nodes, which contain GameStates, which themselves contain a list of the players (and all their…
awrg
  • 31
  • 2
3
votes
1 answer

Self removable resource from flyweight

I'm working on application with different resources which I need to close, while working with reactive streams. I've got factory based on flyweight pattern which keeps references to objects, and they implements AutoCloseable interface. Problem is…
3
votes
1 answer

How does dom4j library implements Flyweight pattern?

I can see in dom4j library a number of classes with Flyweight prefix: FlyweightAttribute, FlyweightComment, FlyweightText etc. Here is what java doc is saying in the case of FlyweightText: FlyweightText is a Flyweight pattern implementation of a…
Dan
  • 11,077
  • 20
  • 84
  • 119
3
votes
1 answer

How is the same object returned multiple times in flyweight design pattern

I have an example which demonstrates flyweight design pattern, where there are soldiers in a game. This soldier instance has to be used by multiple clients simultaneously. And thislink says the same object is returned if it is available in the map…
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41