node.js 前后分离_使用Node.js中的事件实现关注点分离

node.js 前后分离

Separation of Concerns (SoC) one of the important concepts in software design and architecture, it's about decomposing the system into parts that highly independent and slightly overlapping in functionality.

关注点分离(SoC)是软件设计和体系结构中的重要概念之一,它涉及将系统分解为高度独立且功能上有些重叠的部分。

In the case of OOP languages, encapsulation and inheritance are one of the main ways to achieve SoC. Event-Driven Programming (EDP) can be also a major concept in SoC too, ideally, the modules that are throwing the events should not be aware of the modules that will handle them. In this article, I will take you through implementing EDP in NodeJS with a real-life example.

对于OOP语言, 封装继承是实现SoC的主要方法之一。 事件驱动编程(EDP)也是SoC中的一个主要概念,理想情况下,引发事件的模块不应意识到将处理这些事件的模块。 在本文中,我将通过一个真实的示例指导您在NodeJS中实现EDP。

带有示例的基本概念 (Basic concepts with example)

I will talk about an AI system that processes inputs, move across a couple of states, and do actions accordingly. I can give an example of that in reinforcement learning through the Q-learning algorithm.

我将讨论一个AI系统,该系统处理输入,跨几个状态移动并相应地执行操作。 我可以举一个通过Q学习算法进行强化学习的例子。

While the core idea about the Q-learning algorithm is using rewards to optimize the knowledge base of the agent, then action execution would be another topic that can be separated apart from the learning procedure.

尽管有关Q学习算法的核心思想是使用奖励来优化代理的知识库,但是动作执行将是另一个可以与学习过程分开的主题。

And an example of that is the Pacman game where the actions will be confined in the following list (Move up- Move down — Move left — Move right) and the SoC will be achieved by separating the Q-Learning algorithm implementation and applying actions to the screen to move the Pacman to one of the directions.

其中一个例子是吃豆子游戏,其动作将限制在以下列表中(上移-下移-左移-右移),而SoC将通过分离Q-Learning算法实现并将动作应用于屏幕将吃豆人移至其中一个方向。

Image for post

In my example, I will use a very interesting library to implement the event-driven system:

在我的示例中,我将使用一个非常有趣的库来实现事件驱动的系统:

EventEmitter component inside that library supports the ability to dispatch events through “emit” function and to attach handlers to the events using “on” function, and we will talk next more about these concepts next.

该库中的EventEmitter组件支持通过“ emit”函数分发事件并使用“ on”函数将处理程序附加到事件的功能,接下来我们将进一步讨论这些概念。

An Events-driven system composed usually from the following parts:

事件驱动的系统通常由以下部分组成:

  • Event: The type of event that can occur inside the system, in our case (Top, bottom, Left, Right) and that can be implemented using an enum, I implemented that inside events.js file that exports the following object which composed of only key-value pairs, a good practice would be to use Object.freeze() on that object if you don’t want anyone by mistake to modify on it:

    事件:系统内部可能发生的事件类型(在我们的情况下(上,下,左,右),可以使用枚举实现),我实现了在events.js文件内部实现的功能,该文件导出以下对象,该对象由仅键值对,如果您不希望任何人错误地对其进行修改,则好的做法是在该对象上使用Object.freeze()

Image for post
  • Event Handler: a function that will be attached to the event, so whenever the dispatcher dispatched that event, the function will be fired to handle that event with specific logic, I will implement that through a class composed of handlers as functions, and in the initialize function I am passing the dispatcher and link the events handlers with that dispatcher according to event types as next:

    事件处理程序:一个将附加到事件的函数,因此,每当调度程序调度该事件时,都会触发该函数以使用特定逻辑来处理该事件,我将通过一个由处理程序组成的类作为函数来实现该事件,并在初始化函数我正在传递调度程序,并根据以下事件类型将事件处理程序与该调度程序链接:

Image for post
  • Event Dispatcher: The core that will dispatch the events according to a specific context, next is a quick example of that:

    Event Dispatcher:将根据特定上下文调度事件的核心,接下来是一个快速的示例:

Image for post

So to summarize that quickly, QCore is my main component that is responsible for the Q-Learning algorithm and deciding the next states and just dispatching the action that should be taken. Then any other handler can listen to these events to move the Pacman, to do some logging, to send a message to someone…etc. Please note that multiple handlers can be attached to the same events, and it would be great of you encapsulate each group of handlers in a specific class has single responsibility like logging, communication, rendering …etc.

因此,总而言之,QCore是我的主要组件,负责Q-Learning算法并确定下一个状态并只是调度应采取的措施。 然后,其他任何处理程序都可以侦听这些事件,以移动“吃豆人”,进行一些日志记录,向某人发送消息等。 请注意,可以将多个处理程序附加到同一事件上,并且最好将每个处理程序组封装在一个特定类中,该类具有单一职责,例如日志记录,通信,呈现等。

Note: That I used a library to implement the event-driven system, but you can design that by your own, next is a very basic example on how to do that in Javascript:

注意:我使用一个库来实现事件驱动的系统,但是您可以自己设计它,接下来是一个非常基本的示例,说明如何在Javascript中执行此操作:

Image for post

So I believe an example always helps to understand the case faster than theoretical speech. My example was in AI, but you can apply that pattern in any field, most of the time when you have a pipeline to process some inputs and you want also to do some other stuff that is not related to processing business logic, then one of the best solutions is to dispatch event for every step in your pipeline and then design handlers modules that will handle the events according to the other needs.

因此,我相信一个例子总是比理论上的讲话更有助于理解案例。 我的示例是在AI中进行的,但是您可以在任何字段中应用该模式,大多数情况下,当您有管道来处理一些输入并且您还想做其他与处理业务逻辑无关的工作时,可以使用以下一种方法:最好的解决方案是为管道中的每个步骤调度事件,然后设计处理程序模块,这些模块将根据其他需求处理事件。

I hope this quick overview of this pattern helps someone while starting his new project, cause I know implementing that on a pre-designed project is not easy, will include a lot of refactoring and testing :) But believe me you will be more than happy if your project has a lot of overlaps in functionality.

我希望这种模式的快速概述对某人启动新项目有所帮助,因为我知道在预先设计的项目上实现该目标并不容易,将包括大量的重构和测试:)但是相信我,您会很高兴如果您的项目在功能上有很多重叠。

翻译自: https://medium.com/@a.mhaish/achieving-separation-of-concerns-using-events-in-nodejs-40e23b1d5c3b

node.js 前后分离

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值