Layered Pattern

Inseok Chang
2 min readApr 24, 2023

--

So far I made my APIs in a single app.js . Having about 6 different blocks of code inside it, I still found it extremely uncomfortable and tiring to my eyes when I was looking for a specific code block. What if I had 100 instead of 6? How could I organize all that code? I tried to organize it in a CRUD convention where the top codes would be related to ‘create’ and the bottom codes would be about ‘delete’. But that method was still very ineffective. Then I realized the need to learn how to organize these codes. If I continued to write codes like this, it would create a jumble of Spaghetti Codes . Then I learned that there were different architecture patterns in organizing codes. Layered Pattern was the most used and most effective for companies that use IT communication.

The benefits of using Layered Pattern is the following:

  • Scalability: Since each and every layers are independent from each other, we can minimize any effects that is caused by any modifications.
  • Testability: Each layers are independent and does not contain complicated logics, so it’s easier to test certain parts of the code.
  • Readability: Because each layers’ intents and ranges are very clear, it’s easier to understand the structures of the code.
  • Re-usability: Built independently, all the codes can be used for a different framework.

The app.js that I have can be made into different layers. It would be divided into a Presentation layer, Business layer and the Persistence layer each named controller, service, and DAO respectively. Each has a distinct purpose and that’s what makes them separated. Each layers act as a "middleware" . For every request, it must be passsed through every single layer without skipping one.

  1. Presentation Layer (controller)

Presentation layer is a connection between the system and the client. It justifies the endpoints reads all the HTTP requests, which is then sent to the business layer.

2. Business Layer (service)

This layer contains all the logics a business wants to add. For example, it could be the format of the password or what usernames is allowed or not. All the logic of how the business wants to be ran is contained here.

3. Persistence Layer (DAO)

All the database related logics are contained here.

Some important concepts to keep in mind for Layered Pattern is Separation of Concerns (SoC).

  • All the layers are independent from each other.
  • All the layers have different roles.
  • All the layers cannot have overlapping rules.

Now I’m going to try to apply Layered Pattern to the APIs I created!

--

--