The Flyweight pattern is used to minimize memory usage by sharing as much data as possible with similar objects.
class Flyweight {
constructor(make, model, processor) {
this.make = make;
this.model = model;
this.processor = processor;
}
}
class FlyweightFactory {
constructor() {
this.flyweights = {};
}
get(make, model, processor) {
const key = `${make}-${model}-${processor}`;
if (!this.flyweights[key]) {
this.flyweights[key] = new Flyweight(make, model, processor);
}
return this.flyweights[key];
}
getCount() {
return Object.keys(this.flyweights).length;
}
}
class Computer {
constructor(make, model, processor, memory, tag) {
this.flyweight = FlyweightFactory.get(make, model, processor);
this.memory = memory;
this.tag = tag;
}
}
class ComputerCollection {
constructor() {
this.computers = {};
this.count = 0;
}
add(make, model, processor, memory, tag) {
this.computers[tag] = new Computer(make, model, processor, memory, tag);
this.count++;
}
get(tag) {
return this.computers[tag];
}
getCount() {
return this.count;
}
}-
Flyweight:- Represents the shared part of the object. In this case, it includes
make,model, andprocessor. - Constructor initializes these properties.
- Represents the shared part of the object. In this case, it includes
-
FlyweightFactory:- Manages the flyweight objects.
- Contains a method
getto retrieve an existing flyweight or create a new one if it doesn't exist. getCountmethod returns the number of unique flyweights created.
-
Computer:- Represents the full object, which includes both shared (flyweight) and unique parts.
- Constructor initializes the flyweight part using
FlyweightFactory.getand also initializes unique properties likememoryandtag.
-
ComputerCollection:- Manages a collection of
Computerobjects. addmethod adds a new computer to the collection.getmethod retrieves a computer by its tag.getCountmethod returns the number of computers in the collection.
- Manages a collection of
const factory = new FlyweightFactory();
const computers = new ComputerCollection();
computers.add("Dell", "XPS", "Intel", "16GB", "1");
computers.add("Dell", "XPS", "Intel", "8GB", "2");
computers.add("HP", "Envy", "AMD", "16GB", "3");
computers.add("HP", "Envy", "AMD", "8GB", "4");
console.log(`Computers: ${computers.getCount()}`);
console.log(`Flyweights: ${factory.getCount()}`);- A
FlyweightFactoryinstance is created to manage flyweights. - A
ComputerCollectioninstance is created to manage computers. - Several computers are added to the collection with varying memory and tags but shared make, model, and processor.
- The total number of computers and flyweights are logged to the console.
The Flyweight pattern is effectively used here to reduce memory usage by sharing common data (make, model, processor) among multiple Computer objects. The FlyweightFactory ensures that only one instance of each unique combination of make, model, and processor is created and reused. This pattern is particularly useful when dealing with a large number of objects that share common data.