상속관계가있는 두 클래스에서 상위클래스는 뼈대 , 하위클래스는 객체생성에 관한 구체적인 내용을 결정하는 패턴
class Wizard {
constructor() {
this.skill = 'fire ball';
}
}
class Warrior {
constructor() {
this.skill = 'cut';
}
}
class WizardFactory {
static createHero() {
return new Wizard();
}
}
class WarriorFactory {
static createHero() {
return new Warrior();
}
}
const factoryList = { WizardFactory, WarriorFactory };
class HeroFactory {
static createHero(type) {
const factory = factoryList[type];
return factory.createHero();
}
}
const wizard = HeroFactory.createHero('WizardFactory');
const warrior = HeroFactory.createHero('WarriorFactory');
console.log(wizard.skill); // fire ball
console.log(warrior.skill); // cut