OOP/TypeScript的替代方案扩展,在依赖关系中维护父上下文
我创建了一个类eventbus。此类提供了一个用于连接/关/关以接触到类的接口,将其扩展的类可以依次提供一系列可能被侦听的可能事件。
目前,为了使它起作用,我需要使用EventBus扩展课程。我觉得这不是正确的方法。
class Something extends EventBus {
private events: Array<string> = [
'ready',
];
constructor() {
this.registerEventBus(this.events);
}
}
尽管也可以使用构造函数,但使用方法而不是构造函数初始化了EventBus。
class EventBus {
private eventListeners: EventListeners = {};
registerEventBus(eventListenersArray: Array<string> = []) {
eventListenersArray.forEach((listener) => {
this.eventListeners[listener] = [];
});
}
...on, off, supporting private methods etc
我想知道正确的方法是什么。我觉得通过EventBus扩展某些内容不是正确的继承,EventBus是使用的依赖性。我的问题是事件应该是可链的,在每次呼叫后返回正确的上下文;
const someInstance = new Something();
someInstance
.on('ready', () => {})
.on('destroyed', () => {});
目前,我很难找到适用于打字稿的解决方案,我知道它应该被注入,但我没有这样做。如果有人知道我应该采用的方法的正确术语,或者我可以用来规避扩展
的代码示例,我将非常感激!
I've created a class EventBus. This class provides an interface for attaching on/off/once methods to a class, the class extending it can in turn provide an array of possible events that can be listened to.
Currently, to make it work I need to extend the class with EventBus. I feel this isn't the right approach;
class Something extends EventBus {
private events: Array<string> = [
'ready',
];
constructor() {
this.registerEventBus(this.events);
}
}
EventBus is initialised using a method, not a constructor, though a constructor can also be used;
class EventBus {
private eventListeners: EventListeners = {};
registerEventBus(eventListenersArray: Array<string> = []) {
eventListenersArray.forEach((listener) => {
this.eventListeners[listener] = [];
});
}
...on, off, supporting private methods etc
I'm wondering what the right approach for this is. I feel extending something with EventBus isn't correct inheritance, EventBus is a dependency that's used. My issue is that events should be chainable, returning the correct context after each call;
const someInstance = new Something();
someInstance
.on('ready', () => {})
.on('destroyed', () => {});
Currently I struggle to find a solution to this that works with typescript, I know it should be injected but I fail to do so. If anyone know the correct term for the approach I should take, or a sample of code that I can use to circumvent the extends
, I would be very grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您是对的,不应将发布订户模式继承来通过应用程序使用。
您可以使用ioc容器或创建一个单一的实例
您的活动巴士。
typescriptcript 中的单例模式的一个示例:
并使用它:
创建可以看到事件总线的单个实例在这里
更新:
如果要每个类实例的事件总线实例,则可以在类中创建事件总线的实例:
更新1:
如果需要
EventBus
可以通过类的层次结构获得,然后您可以创建类的层次结构。例如,“动物”,“狗”,只是在基类“动物”中宣布活动巴士。通过这样做,方法将在所有派生类型中可用。并像这样运行它:
更新2:
如果要使用可链方法,则可以使用 fluent接口模式。让我展示一个代码的示例:
yeah, you are right that publish subscriber pattern should not be inherited to be used through application.
You can use ioc container or create a single instance of
your event bus.
An example of singleton pattern in TypeScript:
and use it like this:
An example of creating single instance of event bus can be seen here
UPDATE:
If you want to have an instance of event bus per instance of class, then you can create instance of event bus in your class:
UPDATE 1:
If you want
EventBus
to be available through hierarchy of classes, then you can create some hierarchy of your classes. E.g. "Animal", "Dog" and just declare event bus in base class "Animal". By doing this, methods will be available in all derived types.and run it like this:
UPDATE 2:
If you want to have chainable methods, then you can use Fluent interface pattern. Let me show an example of code: