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
3
votes
1 answer

Java Flyweight pattern: Extrinsic and Intrinsic states?

I am confused between the difference of these states for the Flyweight pattern. I am aware that intrinsic state is the state that is shared and that Extrinsic is not. However I do not see the importance that extrinsic state has within the pattern or…
user3287264
3
votes
3 answers

XML DOM Parsing using Flyweight Design

Processing XML documents in java using DOM can be overly memory intensive for document that contain a large number of repetitive nodes, attributes, or values. Does anyone know of an Java XML DOM API that leverages the flyweight-pattern internally…
Chris Johnson
  • 2,631
  • 2
  • 18
  • 17
3
votes
1 answer

Does boost::flyweight do reference counting?

I've been reading the documentation of boost::flyweight but i don't see any mention to deallocation or reference counting policies. Basically a flyweight object should behave like a repository of distinct values, but its not clear what happens when…
lurscher
  • 25,930
  • 29
  • 122
  • 185
2
votes
2 answers

How can I design Flyweight pattern?

Now I'm studying about Flyweight design pattern. I tried to find some examples through google, and this is what I found. import java.awt.Color; import java.awt.Graphics; public interface Shape { public void draw(Graphics g, int x, int y, int…
Eric Kim
  • 21
  • 1
2
votes
2 answers

What is a good example of the Flyweight Pattern?

String internment in c# is a good example. Others?
Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
2
votes
1 answer

Flyweight pattern dilemma

This Flyweight DP tutorial shows a good explanation of the Flyweight pattern. Anyway, let's say that we want to change the font size of the this.height = 200; (assume that the properties are not protected). That means that all the font sizes will be…
john
  • 35
  • 2
2
votes
1 answer

Flyweight design pattern with Swift SceneKit. Object-reuse

I'm building an augmented-reality iPhone application, and it will require a large amount of SceneKit nodes to be rendered. I want to integrate the Flyweight design pattern described in Design Patterns by Gamma, Helm, Johnson, and Vlissides. Also…
Miket25
  • 1,895
  • 3
  • 15
  • 29
2
votes
1 answer

How does the Flyweight pattern in Java work with respect to memory management?

I feel like I have an ok grasp on the design pattern, however I can't seem to get my head around one thing and it doesn't seem to be exactly explained in the resources I've looked at. That question is, how is the data common, without the data being…
adickinson
  • 553
  • 1
  • 3
  • 14
2
votes
1 answer

Flyweight pattern and C++ templates

I have flyweight pattern. I have abstract class Glyph. I have class Letter and abstract Code derived from Glyph. I have YusciiCode, UniCyrCode and UniLatCode derived from Code. My flyweight factory can be done like this: template
2
votes
1 answer

Skipping superfluous verification with immutable flyweights

I have an immutable class that looks something like this: final class Foo { private final String name; private final MutableObject mo; public Foo(String name, MutableObject mo) { mo = mo.clone(); if(!Foo.testValidity(mo)) // this test…
Kevin
  • 1,870
  • 2
  • 20
  • 22
2
votes
1 answer

How do you choose intrinsic state and extrinsic state in flyweight design pattern?

Lets take this question as an example because I am fed up of understanding stereotype examples. A newspaper company is planning to develop a new software system for managing different news articles written by its own journalist and news…
Gayan Jayasingha
  • 752
  • 2
  • 17
  • 33
2
votes
3 answers

Using Flyweight Pattern in database-driven application

Can anyone please give me any example of situation in a database-driven application where I should use Flyweight pattern? How can I know that, I should use flyweight pattern at a point in my application? I have learned flyweight pattern. But not…
user366312
  • 16,949
  • 65
  • 235
  • 452
2
votes
2 answers

C# Processing same object with different "processors" a flyweight pattern?

I've been doing a lot of research on different design patterns and I'm trying to determine the correct way of doing this. I have an image uploading MVC app that I'm developing which needs to process the image in several different ways, such as…
Chris
  • 257
  • 2
  • 14
2
votes
1 answer

Does Serialization allow the Flyweight design pattern?

I'm using a complicated object design where I use the Flyweight pattern to limit certain non-static variables between multiple instances of a class. If both objects (the flyweight info and the class containing it) are Serializable, and a list of…
Mike Beck
  • 35
  • 4
2
votes
3 answers

how flyweight design pattern minimizes memory

How does the Flyweight design pattern minimizes memory used? For me it looks like instead of creating objects it is only externalizing the data held by object which means memory used is the same. Am I missing anything ?
user1743514
  • 311
  • 1
  • 3
  • 21