Skip to content

Instantly share code, notes, and snippets.

@andrewborisov
Created August 18, 2020 06:35
Show Gist options
  • Save andrewborisov/a4d5e2d0a56f568f40044d77297328db to your computer and use it in GitHub Desktop.
Save andrewborisov/a4d5e2d0a56f568f40044d77297328db to your computer and use it in GitHub Desktop.
Гист для статьи "Еще раз о паттернах проектирования в Javascript es6 (часть 2)"
class Flyweight {
constructor(state) {
this.state = state;
}
}
class FlyWeightFactory {
constructor(flyweightsList) {
this.flyweights = {};
flyweightsList.forEach((item) => {
this.flyweights[item.join('_')] = new Flyweight(item);
});
}
getFlyweight(state) {
const key = state.join('_');
if (!(key in this.flyweights)) {
console.log('FlyweightFactory: Не могу найти flyweight, создаю новый.');
this.flyweights[key] = new Flyweight(state);
} else {
console.log('FlyweightFactory: Переиспользую имеющийся flyweight.');
}
return this.flyweights[key];
}
listFlyweights() {
const count = Object.keys(this.flyweights).length;
console.log(`FlyweightFactory: Записано ${count} flyweights:`);
for (const key in this.flyweights) {
console.log(key);
}
}
}
const testing = () => {
const computers = new FlyWeightFactory([['Dell', 'Studio XPS', 'Y755P'], ['Dell', 'Studio XPS', 'X997T']]);
computers.listFlyweights();
computers.getFlyweight(['Dell', 'Studio XPS', 'Y755P']);
computers.getFlyweight(['Dell', 'Studio XPS', 'X997T']);
computers.getFlyweight(['Dell', 'Studio XPS', 'NT777']);
computers.getFlyweight(['HP', 'Envy', 'CNU883701']);
computers.getFlyweight(['HP', 'Envy', 'TXU003283']);
computers.listFlyweights();
};
testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment