自动生成工厂方法

原文链接

将抽象类绑定到自动生成的工厂方法

@injectable()
class Ninja implements Ninja {

    private _katana: Katana;
    private _shuriken: Shuriken;

    public constructor(
	    @inject("Factory<Katana>") katanaFactory: interfaces.Factory<Katana>,
	    @inject("Shuriken") shuriken: Shuriken
    ) {
        this._katana = katanaFactory();
        this._shuriken = shuriken;
    }

    public fight() { return this._katana.hit(); };
    public sneak() { return this._shuriken.throw(); };

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
container.bind<Katana>("Katana").to(Katana);

container.bind<interfaces.Factory<Katana>>("Factory<Katana>")
	  .toAutoFactory<Katana>("Katana");
1
2
3
4
上次更新: 1/5/2020, 1:11:10 PM