Chapters 15 & 16:What Is Architecture?&Independence_《clean architecture》notes

发布于:2025-04-03 ⋅ 阅读:(20) ⋅ 点赞:(0)


Chapter 15: What Is Architecture?

Key Concepts:
  1. Definition: Architecture is the shape of a system defined by components, dependencies, and boundaries that manage complexity and enable evolution.
  2. Core Goals:
    • Manage Complexity: Decouple components to isolate changes.
    • Keep Options Open: Delay decisions (e.g., frameworks, databases) to avoid premature constraints.
    • Support Use Cases: Ensure the system delivers business value.
  3. Layers & Boundaries:
    • Separate high-level policy (business logic) from low-level details (I/O, UI).
    • Use boundaries (interfaces, abstractions) to isolate volatile components.
Code Example: Layered Architecture:
#include <iostream>
#include <string>
#include <vector>

// High-level Policy (Business Logic)
class Order {
   
public:
    virtual double calculateTotal() const = 0;
    virtual ~Order() = default;
};

// Low-level Detail (Implementation)
class ConcreteOrder : public Order {
   
private:
    std::vector<double> items;
public:
    void addItem(double price) {
    items.push_back(price); }
    double calculateTotal() const override {
   
        double total = 0;
        for (auto price : items) total += price;
        return total;
    }
};

// Client Code (Depends on Abstraction)
void printTotal(const Order& order) {
   
    std::cout << "Total: $" << order.calculateTotal() << std::endl;
}

int main() {
   
    ConcreteOrder order;
    order.addItem(10.5);
    order.addItem(20.3);
    printTotal(order); // Output: Total: $30.8
    return 0;
}

Explanation:

  • Abstraction: Order is an interface defining business rules.
  • Implementation: ConcreteOrder provides the calculation logic.
  • Dependency Inversion: printTotal depends on the abstract Order, not concrete details.

Chapter 16: Independence

Key Concepts:
  1. Component Independence:
    • Deployability: Components can be deployed separately (e.g., microservices).
    • Developability: Teams work independently on components.
    • Replaceability: Swap implementations without breaking the system.
  2. Decoupling Techniques:
    • Dependency Inversion: Depend on abstractions, not concretions.
    • Interface Segregation: Split interfaces to avoid unnecessary dependencies.
    • Boundary Patterns: Use layers, ports/adapters, or hexagonal architecture.