Skip to content

Instantly share code, notes, and snippets.

@JeanBarriere
Last active March 11, 2020 12:00
Show Gist options
  • Save JeanBarriere/34f2ff839899928c46cd70380cf4c782 to your computer and use it in GitHub Desktop.
Save JeanBarriere/34f2ff839899928c46cd70380cf4c782 to your computer and use it in GitHub Desktop.
real ts factory
export default abstract class AService {
constructor () {
console.log('service created')
}
}
import AService from './AService'
export class Log extends AService {}
// 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'
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
}
}
}
import ServiceFactory from './ServiceFactory'
console.log(ServiceFactory.get('test'))
console.log(ServiceFactory.get('Log'))
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