Layered Pattern
Learn about the layered architecture pattern.
Before moving on to discuss the layer architectural pattern, we should discuss how architectural patterns differ from design patterns.
Architectural patterns
Architectural patterns are used to organize the components of the code at a very high level. Architectural patterns operate at a higher level and are more concerned about how the whole works, how the data flows, and other structural concerns. The architectural pattern is one level above the design pattern. Design patterns are applied at the class level, and architectural patterns are applied at the higher level.
Layered architecture
Layered architecture is one of the most common architecture patterns, also known as the n-tier architecture pattern. Most developers and architects have used it.
In layered architecture, the components are represented as horizontal layers. Each layer will perform a specific role. Generally, the layered architecture consists of four layers:
-
Presentation Layer: As the name suggests, the presentation layer is responsible for displaying the information to the user and the beginning of each CRUD operation flow.
-
Business Layer: This layer contains the business logic of the application, e.g., in an e-commerce application, this will handle logic like, If the customer has bought something, send an email with their cart details.
-
Data Access Layer: As is obvious from the name, this layer is responsible for interacting with the databases. It can also be called a persistence layer.
-
Database Layer: This is the layer where we would store all the data related to our app.
Note: It is not necessary to only have four layers. It can vary according to the use case.
Open vs. closed layer
A layer is said to be closed when it can’t be bypassed. For example, if the business layer must go through the persistence layer to access the database layer, then the persistence layer will be known as the closed layer. On the other hand, if the business layer doesn’t need to go through the persistence layer to access the database layer, then the persistence layer will be known as the open layer.
Advantages of layered architecture
Layered architecture has many benefits:
- It draws a clear boundary between different types of components and helps organize similar code together.
- Having code at different layers also means that changes in one layer will not affect the code in another layer.
Disadvantages of layered architecture
- The whole system is deployed as a single unit, which means a change in one layer will require us to redeploy the whole application again.
- An application based on layered architecture will be very difficult to scale. The larger the application is, the more resources it will require for each user to go through multiple layers, which will cause latency issues in the application.
Related design patterns
- The microkernel pattern can be considered a special layered architecture.
- The presentation-abstraction-control pattern uses several layers of increasing abstraction.
Get hands-on with 1200+ tech skills courses.