(github)[https://github.com/fbeline/Design-Patterns-JS]
Last active
July 2, 2018 12:36
-
-
Save savchukoleksii/5cb69e805ebe447e69e33187b0fb710f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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