Skip to content

Instantly share code, notes, and snippets.

@kulakowka
Created August 18, 2015 02:02
Show Gist options
  • Save kulakowka/dadde9da3aaf15aa3d84 to your computer and use it in GitHub Desktop.
Save kulakowka/dadde9da3aaf15aa3d84 to your computer and use it in GitHub Desktop.
ObjFactory
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