Created
June 29, 2020 10:10
-
-
Save storyn26383/3d4abd5a6d870514bfadecc767547c12 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
class Container { | |
static get proxyHandler () { | |
return { | |
get (target, prop) { | |
if (prop in target) { | |
return Reflect.get(...arguments) | |
} | |
return target.resolve(prop) | |
} | |
} | |
} | |
static getInstance () { | |
if (!this.instance) { | |
this.instance = new Proxy(new this, this.proxyHandler) | |
} | |
return this.instance | |
} | |
constructor () { | |
this.bindings = [] | |
this.instances = [] | |
} | |
singleton (abstract, concrete) { | |
this.bindings[abstract] = concrete | |
} | |
instance (abstract, instance) { | |
this.instances[abstract] = instance | |
} | |
resolve (abstract) { | |
if (abstract in this.bindings && !(abstract in this.instances)) { | |
this.instances[abstract] = this.bindings[abstract]() | |
} | |
return this.instances[abstract] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment