Achieving separation of concerns using Events in Node.js

Achieving separation of concerns using Events in 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.

*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.

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.

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.

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.

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.

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.

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:

software-engineering software-development programming javascript nodejs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer]( "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Developer or Engineer? Does It Make a Difference?

To summarise the main differences between the software developer and engineer: A developer executes. ... So the software developer is mainly focused on developing code that is a part of software development cycle. An engineer designs and plans applying the principles of engineering to software development.

5 Core Criteria for Selecting Software Development Company -

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.