Software architecture

Layered architecture

Pattern Description:

Components within the layered architecture pattern are organized into horizontal layers, each layer performing a specific role within the application (e.g., presentation logic or business logic). Although the layered architecture pattern does not specify the number and types of layers that must exist in the pattern, most layered architectures consist of four standard layers: presentation, business, persistence, and database

Each layer of the layered architecture pattern has a specific role and responsibility within the application. For example, a presentation layer would be responsible for handling all user interface and browser communication logic, whereas a business layer would be responsible for executing specific business rules associated with the request. Each layer in the architecture forms an abstraction around the work that needs to be done to satisfy a particular business request. For example, the presentation layer doesn’t need to know or worry about how to get customer data; it only needs to display that information on a screen in particular format.

Features:

1. Separation of Concerns:
Separation of concerns means dividing the system into distinct sections (layers), each with a specific responsibility.

Separation of concerns features:

  • Modular Design: Each layer addresses a specific aspect of the application’s functionality (e.g., presentation, business logic, data access).

  • Focused Development: Developers can focus on one layer at a time, making it easier to manage complexity

2. Defined Layer Responsibilities:

Each layer has a well-defined role and scope of responsibility

Example:

  • Presentation Layer: Handles the user interface and user interaction. Examples include web pages, mobile app screens, or command-line interfaces.

  • Business Logic Layer: Implements the core functionality of the application, including business rules and data processing.

  • Persistence Layer: Manages data access, including database operations and communication with other data storage services.

  • Database Layer: Directly interacts with the database to perform CRUD (Create, Read, Update, Delete) operations.

3. Encapsulation:

Each layer hides its internal workings from the other layers, exposing only what is necessary.

Encapsulation features:

  • Abstraction: Layers provide interfaces or APIs that abstract the underlying implementation details.

  • Controlled Access: Other layers interact with a layer through its defined interfaces, promoting loose coupling.

4. Reusability:

Layers can be reused across different parts of the application or even in different applications.

Reusability features:

  • Common Components: Shared services, utilities, and components can be placed in a common layer to be reused by multiple higher-level layers.

  • Reusable Logic: Business rules and data access logic can be reused across different presentation interfaces.

5. Maintainability:

The architecture makes it easier to maintain and extend the system over time.

Maintainability features:

  • Isolated Changes: Changes in one layer generally do not impact other layers, minimizing the risk of unintended side effects.

  • Clear Structure: The well-defined structure aids in understanding, testing, and debugging the system.

6. Scalability:

The system can grow in capacity and complexity while maintaining performance and manageability.

Scalability features:

  • Horizontal Scalability: Layers can be scaled independently. For example, the presentation layer can be scaled to handle more user requests, while the database layer can be scaled to handle more data.

  • Layer-Specific Optimizations: Performance optimizations can be applied to specific layers based on their unique requirements.

7. Flexibility:

The architecture supports changing and extending the application with minimal impact on existing code.

Flexibility features:

  • Easier Integration: New functionalities or components can be added to the appropriate layer without affecting others.

  • Adaptability: The system can adapt to new requirements, technologies, or platforms more easily due to the separation of concerns.

8. Testability:

The architecture supports effective testing strategies.

Testability features:

  • Unit Testing: Each layer’s components can be tested independently using unit tests.

  • Mocking Dependencies: Mocking frameworks can be used to simulate interactions with other layers, facilitating isolated testing.

  • Integration Testing: Layers can be tested together to ensure they interact correctly.

9. Performance Considerations:

The architecture should account for performance impacts due to layer traversal.

Performance considerations features:

  • Minimized Overhead: Care should be taken to avoid unnecessary processing or pass-through scenarios where layers do not add value (addressing the architecture sinkhole anti-pattern).

  • Optimized Communication: Efficient communication protocols and data handling strategies should be employed to minimize latency and overhead.

10. Security:

The architecture helps in implementing security measures across the application.

Security features:

  • Layer-Specific Security: Different layers can implement security measures appropriate to their function (e.g., input validation in the presentation layer, access controls in the business layer).

  • Centralized Security Policies: Common security concerns (like authentication and authorization) can be handled in a dedicated layer or service.

Key Concepts

Closed layer:

A closed layer means that as a request moves from layer to layer, it must go through the layer right below it to get to the next layer below that one.

For example, a request originating from the presentation layer must first go through the business layer and then to the persistence layer before finally hitting the database layer.

Layers of isolation:

The concept of layers of isolation within the layered architecture pattern refers to the practice of designing each layer to be independent of the others in terms of functionality and implementation. This isolation ensures that changes or issues in one layer do not directly impact other layers, promoting a modular and maintainable system.

Open layers:

When layer marked as open that's meaning requests are allowed to bypass this open layer and go directly to the layer below it.

In the following example, since the services layer is open, the business layer is now allowed to bypass it and go directly to the persistence layer.

Architecture Sinkhole Anti-Pattern:

The architecture sinkhole anti-pattern occurs when the layered architecture has layers that do not add significant value or processing but merely pass requests and responses between layers. This leads to inefficiencies and can undermine the benefits of a well-structured layered architecture.