Created
August 18, 2015 02:02
-
-
Save kulakowka/dadde9da3aaf15aa3d84 to your computer and use it in GitHub Desktop.
ObjFactory
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 ObjFactory { | |
constructor(author) { | |
this.author = author; | |
} | |
createCar(model) { | |
return new Car(this.author, model); | |
} | |
createHuman(name) { | |
return new Human(this.author, name); | |
} | |
createBook(title) { | |
return new Book(this.author, title); | |
} | |
} | |
class Car { | |
constructor(author, model) { | |
this.type = 'Car'; | |
this.author = author; | |
this.model = model; | |
console.log(this); | |
} | |
} | |
class Human { | |
constructor(author, name) { | |
this.type = 'Human'; | |
this.author = author; | |
this.name = name; | |
console.log(this); | |
} | |
} | |
class Book { | |
constructor(author, title) { | |
this.type = 'Book'; | |
this.author = author; | |
this.title = title; | |
console.log(this); | |
} | |
} | |
var factory = new ObjFactory('kulakowka'); | |
var car1 = factory.createCar('nissan 350z'); | |
var car2 = factory.createCar('mazda 3'); | |
var human1 = factory.createHuman('Vladimir Putin'); | |
var human2 = factory.createHuman('Dmitry Medvedev'); | |
var book1 = factory.createBook('War and peace'); | |
var book2 = factory.createBook('Js for dummy'); | |
/** | |
{ type: 'Car', author: 'kulakowka', model: 'nissan 350z' } | |
{ type: 'Car', author: 'kulakowka', model: 'mazda 3' } | |
{ type: 'Human', author: 'kulakowka', name: 'Vladimir Putin' } | |
{ type: 'Human', author: 'kulakowka', name: 'Dmitry Medvedev' } | |
{ type: 'Book', author: 'kulakowka', title: 'War and peace' } | |
{ type: 'Book', author: 'kulakowka', title: 'Js for dummy' } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment