Skip to content

Instantly share code, notes, and snippets.

@josser

josser/factory2.ts

Created Mar 31, 2020
Embed
What would you like to do?
interface StoreOptions {
}
interface DbStoreOptions extends StoreOptions {
dsn: string
}
interface FileStoreOptions extends DbStoreOptions {
path: string
}
interface IStore {
get(): void;
}
class DbStore implements IStore {
constructor(options: DbStoreOptions) { }
get() {
console.log("beep beep");
}
}
class FileStore implements IStore {
constructor(options: FileStoreOptions) { }
get() {
console.log("tick tock");
}
}
function factory(storeName: string, storeOptions: StoreOptions): IStore {
const storeMap: {
[index: string]: any
} = { file: FileStore, db: DbStore }
return new storeMap[storeName](storeOptions)
}
const store = factory('file', { path: 'test' })
store.get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment