Skip to content

Instantly share code, notes, and snippets.

@savchukoleksii
Last active July 2, 2018 12:36
Show Gist options
  • Save savchukoleksii/5cb69e805ebe447e69e33187b0fb710f to your computer and use it in GitHub Desktop.
Save savchukoleksii/5cb69e805ebe447e69e33187b0fb710f to your computer and use it in GitHub Desktop.
class Figure{
constructor(){
this.name = "Figure";
}
}
class Pawn extends Figure{
constructor(){
super();
this.name = "Pawn";
}
}
class King extends Figure{
constructor(){
super();
this.name = "King";
}
}
class Queen extends Figure{
constructor(){
super();
this.name = "Queen";
}
}
const Factory = {
registeredTypes: new Map(),
register(className, classReference) {
if (!(Factory.registeredTypes.has(className) && classReference.prototype instanceof Figure)) {
Factory.registeredTypes.set(className, classReference);
} else {
}
},
create(className, ...options) {
if (!Factory.registeredTypes.has(className)) {
console.error("!!!");
return null;
}
let classReference = this.registeredTypes.get(className);
let instance = new classReference(...options);
return instance;
}
}
Factory.register("Pawn", Pawn);
Factory.register("King", King);
Factory.register("Pawn", Queen);
console.log(Factory.create("Pawn"));
class Singletone{
constructor(){
if(Singletone.instance){
return Singletone.instance;
}
Singletone.instance = this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment