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

How to reduce RAM usage in java?

ReportDatabase class public class ReportDatabase { private static final Map reportTypes; private static final String[] reportTypeNameVersion = { "Payroll Tax Report 1", "Capital Gains Tax Report 1", …
0
votes
1 answer

Using Threads and Flyweight pattern together in Java?

I'm new to both multi-threading and using design patterns. I've some threads using explicit multi-threading and each is suppose to compute the factorial of a number if it hasn't been computed ever by any thread. I'm using Flyweight Pattern for…
user11129457
0
votes
1 answer

Static factory methods instead of constructors

I've been researching this after I read Joshua Block's Effective Java Book, Item 1, about using factory static methods instead of constructors. There in the text he defends the use, whenever possible and cites, among other justifications, the…
deldev
  • 1,296
  • 18
  • 27
0
votes
1 answer

Flyweight pattern - how to store flyweights in the data structure?

Classic Flyweight pattern implementation example from GoF book only stores character code in sharable "Characters" and uses "GlyphContext" to store extrinsic state in a tree structure. This example also mentions Rows and Columns, however it doesn't…
Konstantin
  • 3,817
  • 4
  • 29
  • 39
0
votes
0 answers

How to spawn an object using the FlyWeight Pattern

So I've been setting up this test to implement the flyweight pattern. It's where I create an object class and a factory to basically spawn the same kind of object with different properties, in this case, color, size, and shape. So I have a dropdown…
user8048595
  • 119
  • 1
  • 2
  • 12
0
votes
1 answer

Javascript Flyweight with WeakMap or WeakSet

I want a Flyweight object so I created an Object and stored it's instances in a Map like this: const FlyweightNumber = (function(){ "use strict"; const instances = new Map(); class FlyweightNumber{ constructor(number){ …
Paulo Henrique
  • 938
  • 1
  • 13
  • 19
0
votes
1 answer

Flyweight Pattern : UnsharedConcreteFlyweight

Here is the flyweight pattern structural diagram: Here you see UnsharedConcreteFlyweight which GoF explains: UnsharedConcreteFlyweight : Not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing; it doesn't…
Narek
  • 38,779
  • 79
  • 233
  • 389
0
votes
1 answer

Having issue with flyweight pattern

We use flyweight when there is need to crate bulk of a particular kind of object. Because they share a common data (intrinsic state) so helps in reducing memory consumption, and also have their own state (extrinsic state) which is not shared among…
user464
  • 255
  • 1
  • 2
  • 10
0
votes
1 answer

How do I use Decorator and Flyweight design patterns together?

In what situation is best used design pattern Decorator and Flyweight together. I have a financial system to build and some friends showed me these standards.
0
votes
1 answer

Flyweight pattern with a bidimensional array

I have a matrix (bidimensional array) of Objects Bee, I want to apply the flyweight pattern, how should I apply it?, if I instantiate the matrix directly with a huge size I would be allocating too much memory. Is there a efficient way to apply the…
0
votes
2 answers

What does influence the most on the time of creating an object?

I know that creating an object takes time and that's why the flyweight pattern exists. What I would like to know is what increases the time of creating a single object the most? I thought it might be the search of the a slightly larger space in…
Vitali Pom
  • 602
  • 1
  • 8
  • 29
0
votes
1 answer

Design a word processor

I came across an interview question asking to design a word processor. After my research I found Flyweight design pattern as an approach. I came up with below code (ignore syntax). But I am having hard time thinking of what will be my key and what…
0
votes
1 answer

Strategy Pattern reusing data to create a report

I am implementing a report that will use different components ie some have header, footer table. Another has header, title, table, graph. I have implemented this using a similar pattern to the strategy pattern. I can generate an report using the…
perkss
  • 1,037
  • 1
  • 11
  • 37
-1
votes
4 answers

How to avoid creation of multiple instances of object?

An employee’s salary is calculated from two elements – Basic and StandardPoint. One employee will get one Basic and one or more (or no) StandardPoint each month. There are various StandardPoints namely – StarPerformerPoint , RecognitionPoint ,…
LCJ
  • 22,196
  • 67
  • 260
  • 418
-2
votes
1 answer

flyweigt pattern and concurrency

How can I ensure thread safety in case of the flyweight design pattern. What are the concurrency issues to be concerned about, Are there any standard solutions to these issues. I am looking for solutions with respect to c++ I was trying the normal…
user3282758
  • 1,379
  • 11
  • 29