Contact Me

Clean Architecture

Separates the system into layers, so that the more important a layer is, the more protected it is

 The great villain of software development is the coupling between components, so the clean architecture has a layer separation model that can be represented by the image below:

cleanArchImg

 The arrows indicate how each layer communicates data with another.

 This separation of concerns allows for a clear separation of business rules from external services and user interface, making the code more modular, testable, and easy to maintain. Additionally, by keeping the core business rules in the center and external services on the outer layers, it makes it easier to change or replace external services without affecting the core business rules. It also makes the system more flexible and adaptable to changing requirements. Overall, the goal of clean architecture is to create a maintainable and scalable software system that is easy to understand, test and modify.


 In summary, the layers could be described as follows:


  1. Entities
  2.  These are the most core and independent classes of the system. They represent the business rules and do not depend on any external services. For example, in an e-commerce application, an Entity might be a Product class with properties such as id, name, and price.

    class Product {

    constructor(id, name, price) {

      this.id = id;

      this.name = name;

      this.price = price;

    }

    }

  3. Use Cases
  4.  These classes handle specific business logic and interact with the Entities. They depend on the Entities, but not on any external services. For example, in an e-commerce application, a Use Case might be a AddProduct class that takes a Product instance and adds it to the cart.

    class AddProduct{

    constructor(cart){

      this.cart = cart;

    }

    execute(product){

      this.cart.add(product);

    }

    }

  5. Interface Adapters
  6.  These classes handle the interaction between the Use Cases and external services such as databases, APIs, and the user interface. They depend on the Use Cases but not on any external services. For example, in an e-commerce application, an Interface Adapter might be a ProductRepository class that takes a Product instance and saves it to a database.

    class ProductRepository{

    constructor(db){

      this.db = db;

    }

    save(product) {

      this.db.save('products', product);

    }

    }

  7. Frameworks & Drivers
  8.  These are the external services such as databases, APIs, and the user interface that the system depends on. They are the outermost layer and depend on the Interface Adapters. For example, in an e-commerce application, a Framework & Driver might be a Express server that handles HTTP requests and responses.

    const express = require('express');

    const app = express();

    app.get('/product/:id', (req, res) => {

    const productId = req.params.id;

    const product = ProductRepository.find(productId);

     res.json(product);

    });