Last active
March 11, 2020 12:00
-
-
Save JeanBarriere/34f2ff839899928c46cd70380cf4c782 to your computer and use it in GitHub Desktop.
real ts factory
This file contains 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
export default abstract class AService { | |
constructor () { | |
console.log('service created') | |
} | |
} |
This file contains 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 AService from './AService' | |
export class Log extends AService {} |
This file contains 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
// now we can export our services | |
// this can be replaced by a loop that get all the files in a folder and export them | |
export * from './Log' |
This file contains 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 * as Services from './services' | |
import AService from './AService' | |
export default class ServiceFactory { | |
static get (name: string): AService | null { | |
try { | |
const service: AService = new Services[name]() | |
return service | |
} catch (e) { | |
console.warn(`${name} is not a service`) | |
return null | |
} | |
} | |
} |
This file contains 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 ServiceFactory from './ServiceFactory' | |
console.log(ServiceFactory.get('test')) | |
console.log(ServiceFactory.get('Log')) |
This file contains 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
test is not a service | |
null | |
service created | |
Log {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment