IOC控制反转 DI依赖注入
作者:一二三
发布时间:2024-10-09 11:01
0人已阅读
简介:
class A {
name: string
constructor(name:string) {
this.name = name;
}
}
class B {
name: string
constructor(name:string) {
this.name = name;
}
}
class Container {
mo: object
constructor() {
this.mo = {}
}
provide(key:string,mo:any){
this.mo[key]=mo;
}
get(key:string){
return this.mo[key];
}
}
const mo = new Container()
mo.provide('a', new A('aaaaa'))
mo.provide('b', new B('bbbbb'))
class C {
a:any
b:any
constructor(){
this.a = mo.get('a')
this.b = mo.get('b')
}
get():string{
return this.a.name + '+' + this.b.name
}
}
const c = new C()
console.log(c.get())