// Flyweight class
class Champion {
constructor(name, role) {
this.name = name;
this.role = role;
}
display() {
console.log(`Displaying ${this.name}, the ${this.role}`);
console.log(`Image: ${this.image}`);
}
}
// Flyweight factory
class ChampionFactory {
constructor() {
this.pool = {};
}
getChampion(name) {
if (!this.pool[name]) {
let role;
if (name === 'Ashe') {
role = '원딜';
} else if (name === 'Lux') {
role = '미드';
} else if (name === 'Garen') {
role = '탑';
} else {
throw new Error(`Invalid champion name: ${name}`);
}
this.pool[name] = new Champion(name, role);
}
return this.pool[name];
}
}
// Client class
class LeagueOfLegends {
constructor() {
this.factory = new ChampionFactory();
this.champions = [];
}
selectChampion(name) {
const champion = this.factory.getChampion(name);
champion.display();
this.champions.push(champion);
}
}
// Usage example
const game = new LeagueOfLegends();
game.selectChampion('Ashe');
game.selectChampion('Lux');
game.selectChampion('Garen');
game.selectChampion('Ashe');