Created
March 31, 2020 12:51
-
-
Save josser/b04201936b1527c830b6536daa9d2ba3 to your computer and use it in GitHub Desktop.
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
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