我不确定在构造函数中传递类本身是否合理,如下所示:
class OngoingActions {
constructor(Activation) {
this._actions = {
onTurnStart: [new Activation(), new Activation()],
onTurnEnd: [new Activation(), new Activation()],
onDraw: [new Activation(), new Activation()],
onMove: new Activation()
};
}
}
我知道对于依赖注入,您需要传递类的实例。在构造函数中传递同一个类的 7 个或更多实例会感觉有点尴尬。我也可以导入整个 '_actions' 对象,但是在同一个类中看到对象的结构感觉很好。处理这个问题的最佳方法是什么?
如果你正在寻找factory pattern
,这意味着工厂类必须知道object class
. 并且generate function
不应该是实例函数,而应该是static
. 所以只有一个source for truth
。
class Activation {
constructor(event) {
this.event = event;
}
}
class OnGoingActionFactory {}
OnGoingActionFactory.getActions = () => {
return {
onTurnStart: [new Activation("start"), new Activation("start2")],
onTurnEnd: [new Activation("end"), new Activation("end")],
onDraw: [new Activation(), new Activation()],
onMove: new Activation(),
};
};
console.log(OnGoingActionFactory.getActions())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句