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