Architectural patterns and design principles for scalable, maintainable systems. Use when designing systems, refactoring architecture, or choosing patterns.
Guide architectural decisions with proven patterns for building scalable, maintainable, and testable systems.
┌─────────────────────────────────────────────────────────────┐
│ Frameworks & Drivers │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ Interface Adapters ││
│ │ ┌─────────────────────────────────────────────────────┐││
│ │ │ Application Business Rules │││
│ │ │ ┌─────────────────────────────────────────────────┐│││
│ │ │ │ Enterprise Business Rules ││││
│ │ │ │ (Domain/Entities) ││││
│ │ │ └─────────────────────────────────────────────────┘│││
│ │ └─────────────────────────────────────────────────────┘││
│ └─────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────┘
Key Principles:
Layer Responsibilities:
| Layer | Contains | Depends On | |-------|----------|------------| | Entities | Business objects, domain logic | Nothing | | Use Cases | Application-specific logic | Entities | | Adapters | Controllers, gateways, presenters | Use Cases, Entities | | Frameworks | Web framework, database, UI | All inner layers |
┌──────────────┐
│ Driving │
│ Adapter │
│ (REST API) │
└──────┬───────┘
│
┌──────▼───────┐
│ Port │
│ (Interface) │
└──────┬───────┘
│
┌──────────────────────────▼──────────────────────────┐
│ Application Core │
│ ┌────────────────────────────────────────────────┐ │
│ │ Domain Model │ │
│ │ (Entities + Business Rules) │ │
│ └────────────────────────────────────────────────┘ │
└──────────────────────────┬──────────────────────────┘
│
┌──────▼───────┐
│ Port │
│ (Interface) │
└──────┬───────┘
│
┌──────▼───────┐
│ Driven │
│ Adapter │
│ (Database) │
└──────────────┘
Key Concepts:
Strategic Patterns:
| Pattern | Purpose | |---------|---------| | Bounded Context | Define clear boundaries between domains | | Ubiquitous Language | Shared vocabulary between devs and domain experts | | Context Mapping | Define relationships between bounded contexts |
Tactical Patterns:
| Pattern | Purpose | Example | |---------|---------|---------| | Entity | Object with identity | User, Order | | Value Object | Immutable, no identity | Money, Address | | Aggregate | Cluster of entities | Order + OrderItems | | Repository | Abstraction for persistence | UserRepository | | Domain Event | Something that happened | OrderPlaced | | Domain Service | Logic not belonging to entity | PaymentProcessor |
When to Use:
When to Avoid:
┌─────────┐ ┌──────────────┐ ┌─────────┐
│ Service │────▶│ Event Broker │────▶│ Service │
│ A │ │ (Kafka/SQS) │ │ B │
└─────────┘ └──────────────┘ └─────────┘
│ │
└──────────── Events ────────────────┘
Patterns:
┌─────────┐ ┌─────────────┐ ┌───────────┐
│ Client │────▶│ API Gateway │────▶│ Service A │
└─────────┘ └──────┬──────┘ ├───────────┤
│ │ Service B │
└───────────▶├───────────┤
│ Service C │
└───────────┘
Gateway Responsibilities:
enum CircuitState {
CLOSED, // Normal operation
OPEN, // Failing, reject calls
HALF_OPEN // Testing recovery
}
class CircuitBreaker {
private state = CircuitState.CLOSED;
private failures = 0;
private threshold = 5;
async execute<T>(fn: () => Promise<T>): Promise<T> {
if (this.state === CircuitState.OPEN) {
throw new CircuitOpenError();
}
try {
const result = await fn();
this.onSuccess();
return result;
} catch (error) {
this.onFailure();
throw error;
}
}
}
async function retryWithBackoff<T>(
fn: () => Promise<T>,
maxRetries: number = 3
): Promise<T> {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(Math.pow(2, i) * 1000); // 1s, 2s, 4s
}
}
}
Isolate failures to prevent cascade:
class Bulkhead {
private semaphore: Semaphore;
constructor(maxConcurrent: number) {
this.semaphore = new Semaphore(maxConcurrent);
}
async execute<T>(fn: () => Promise<T>): Promise<T> {
await this.semaphore.acquire();
try {
return await fn();
} finally {
this.semaphore.release();
}
}
}
interface UserRepository {
findById(id: string): Promise<User | null>;
findByEmail(email: string): Promise<User | null>;
save(user: User): Promise<void>;
delete(id: string): Promise<void>;
}
// Implementation details hidden
class PostgresUserRepository implements UserRepository {
// ...database-specific code
}
interface UnitOfWork {
users: UserRepository;
orders: OrderRepository;
commit(): Promise<void>;
rollback(): Promise<void>;
}
┌──────────────┐ ┌──────────────────┐
│ Commands │────────▶│ Write Model │
│ (Create, │ │ (Normalized) │
│ Update) │ └────────┬─────────┘
└──────────────┘ │
│ Sync
▼
┌──────────────┐ ┌──────────────────┐
│ Queries │────────▶│ Read Model │
│ (GetById, │ │ (Denormalized) │
│ Search) │ └──────────────────┘
└──────────────┘
| Scenario | Recommended Pattern | |----------|---------------------| | Complex domain logic | DDD + Clean Architecture | | High scalability | Microservices + Event-Driven | | Simple CRUD | MVC / Layered Architecture | | Real-time updates | Event Sourcing + CQRS | | Legacy migration | Strangler Fig Pattern | | API aggregation | BFF (Backend for Frontend) |
| Anti-Pattern | Problem | Solution | |--------------|---------|----------| | Big Ball of Mud | No structure | Introduce bounded contexts | | Distributed Monolith | Coupled services | True service boundaries | | Anemic Domain Model | Logic in services | Rich domain model | | Golden Hammer | One pattern for all | Right tool for job | | Premature Abstraction | Over-engineering | YAGNI, iterate |
Before choosing an architecture:
What are the scaling requirements?
What is the team size/structure?
What are the consistency requirements?
What is the expected rate of change?
What are the operational capabilities?
npx skills add mjohnson518/architecture-patterns下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer