Created
June 29, 2015 17:09
-
-
Save codeimpossible/d7c40585cb07249dca46 to your computer and use it in GitHub Desktop.
Abstract Factory, with self-registration!
This file contains hidden or 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
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; | |
} | |
}; |
This file contains hidden or 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
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