Flyweight Design Pattern
Structural: Flyweight Pattern
Can you explain the flyweight design pattern?
View Answer:
Interview Response: It allows you to fit more objects into the available RAM by sharing parts of state between multiple objects rather than keeping all of the data in each object.
Code Example #1:


This pattern's objects are as follows:
Client -- Example code: Computer
- calls into FlyweightFactory to obtain flyweight objects
FlyweightFactory -- In example code: FlyweightFactory
- creates and manages flyweight objects
- If a flyweight is required and one does not exist, it constructs one.
- stores newly created flyweights for future requests
Flyweight -- In example code: Flyweight
- preserves intrinsic data for use throughout the application
function Flyweight(make, model, processor) {
this.make = make;
this.model = model;
this.processor = processor;
}
let FlyWeightFactory = (function () {
let flyweights = {};
return {
get: function (make, model, processor) {
if (!flyweights[make + model]) {
flyweights[make + model] = new Flyweight(make, model, processor);
}
return flyweights[make + model];
},
getCount: function () {
let count = 0;
for (let f in flyweights) count++;
return count;
},
};
})();
function ComputerCollection() {
let computers = {};
let count = 0;
return {
add: function (make, model, processor, memory, tag) {
computers[tag] = new Computer(make, model, processor, memory, tag);
count++;
},
get: function (tag) {
return computers[tag];
},
getCount: function () {
return count;
},
};
}
let Computer = function (make, model, processor, memory, tag) {
this.flyweight = FlyWeightFactory.get(make, model, processor);
this.memory = memory;
this.tag = tag;
this.getMake = function () {
return this.flyweight.make;
};
// ...
};
function run() {
let computers = new ComputerCollection();
computers.add('Dell', 'Studio XPS', 'Intel', '5G', 'Y755P');
computers.add('Dell', 'Studio XPS', 'Intel', '6G', 'X997T');
computers.add('Dell', 'Studio XPS', 'Intel', '2G', 'U8U80');
computers.add('Dell', 'Studio XPS', 'Intel', '2G', 'NT777');
computers.add('Dell', 'Studio XPS', 'Intel', '2G', '0J88A');
computers.add('HP', 'Envy', 'Intel', '4G', 'CNU883701');
computers.add('HP', 'Envy', 'Intel', '2G', 'TXU003283');
console.log('Computers: ' + computers.getCount());
console.log('Flyweights: ' + FlyWeightFactory.getCount());
}
run();
/*
OUTPUT:
Computers: 7
Flyweights: 2
*/
The Flyweight pattern belongs to which pattern family?
View Answer:
Interview Response: The Flyweight pattern gets classified as a Structural design pattern.
What types of objects are involved in the Flyweight Pattern?
View Answer:
Interview Response: The Client, FlyweightFactory, and Flyweight are all part of the Flyweight pattern.
- Client – To obtain flyweight objects, the Client invokes FlyweightFactory.
- FlyweightFactory – If a flyweight object is requested but does not exist, the FlyweightFactory generates and manages it. It spawns one and saves newly generated flyweights for future use.
- Flyweight – stores intrinsic data that gets shared throughout the program.
When should the Flyweight Pattern be used?
View Answer:
Interview Response: We should use this pattern when our application has many objects that consume the same data or when memory storage costs are high. JavaScript uses this pattern to distribute a list of immutable strings throughout the program.
This pattern most commonly gets found in network programs or word processors, and it can be used in internet browsers to prevent the same images from loading. The flyweight pattern enables image caching. As a result, only new images are loaded from the Web when a web page loads, while existing ones get extracted from the cache.
This pattern most commonly gets found in network programs or word processors, and it can be used in internet browsers to prevent the same images from loading. The flyweight pattern enables image caching. As a result, only new images are loaded from the Web when a web page loads, while existing ones get extracted from the cache.
What are some of the advantages of employing the Flyweight pattern?
View Answer:
Interview Response: If your software has a lot of similar objects, you can save much memory.
What are some of the disadvantages of employing the Flyweight pattern?
View Answer:
Interview Response: Drawbacks of the Flyweight Pattern.
- When certain context data needs to be regenerated each time a flyweight method gets called, you may be sacrificing RAM for CPU cycles.
- The code becomes noticeably more complex with the Flyweight Pattern.
- New colleagues get perplexed as to why an entity's state gets partitioned.