Skip to content

Instantly share code, notes, and snippets.

@codeimpossible
Created June 29, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeimpossible/d7c40585cb07249dca46 to your computer and use it in GitHub Desktop.
Save codeimpossible/d7c40585cb07249dca46 to your computer and use it in GitHub Desktop.
Abstract Factory, with self-registration!
let subclasses = {};
export default class BlogPostImporter {
constructor(body) {
for(var p in body) {
if(body.hasOwnProperty(p)) {
this[p] = body[p];
}
}
}
static define(name, body) {
return new BlogPostImporter(body).register(name);
}
static create(type) {
let Klass = subclasses[type];
if(!Klass) {
throw `Bad importer type ${type}`;
}
return new Klass();
}
register(name) {
subclasses[name] = this;
return this;
}
};
import {default as BlogPostImporter} from './blog-post-importer';
export default BlogPostImporter.define('wordpress', {
import: (content, import_types, date_type) => {
// code to import word press posts from XML
},
make_post: (el, use_original_date) => {
// code to insert a new post in our DB
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment