🏛️ Enterprise Software Architecture & Design Patterns
1️⃣ SOLID Principles at Enterprise Scale
Definition: SOLID Robert C. Martin dwara formalize kiye gaye 5 foundational design principles hain jo codebase ko loosely coupled, testable, maintainable aur business growth ke sath linearly extensible banate hain.
| Principle | Core Concept | Architectural Violation & Fix |
|---|---|---|
| S: Single Responsibility (SRP) | Ek class ya module ke paas change hone ka sirf ek hi reason hona chahiye. | OrderService me PDF invoice generation ya email sending code daalna (Violates SRP). Dedicated InvoiceGenerator aur NotificationService me delegate karein. |
| O: Open/Closed (OCP) | Entities extension ke liye open honi chahiye, par modification ke liye closed. | Naye payment provider (Razorpay, Stripe) add karne ke liye core checkout class me giant if-else conditions add karna. Strategy Pattern use karein. |
| L: Liskov Substitution (LSP) | Subtypes ko unke base types ke place par bina system behavior alter kiye substitute kiya ja sake. | Subclass dwara parent interface method me throw new UnsupportedOperationException() fekna (Violates LSP). |
| I: Interface Segregation (ISP) | Clients ko un interfaces par depend hone par majboor nahi kiya jana chahiye jinhe wo use nahi karte. | Fat interfaces todkar fine-grained client-specific interfaces (e.g. Readable, Writable) banana. |
| D: Dependency Inversion (DIP) | High-level business modules low-level infrastructure modules par depend nahi hone chahiye; dono abstractions par depend hone chahiye. | Core business domain class me direct new PostgresRepository() instantiate karna. Abstract OrderRepository interface inject karein. |
2️⃣ Hexagonal Architecture (Ports & Adapters)
Alistair Cockburn dwara create ki gayi architecture jisme business logic application ke center me rehti hai aur external dependencies (Databases, HTTP APIs, Message Queues) se completely decoupled hoti hai:
[Driving / Inbound Adapters] +------------------------------------+ [Driven / Outbound Adapters]
REST API Controller ----> | INBOUND PORTS (Interfaces) | ----> PostgreSQL Database
CLI Console Handler ----> +------------------------------------+ ----> Redis Distributed Cache
Kafka Event Consumer ----> | CORE DOMAIN & BUSINESS LOGIC | ----> Third-Party Payment API
+------------------------------------+ ----> AWS S3 File Storage
| OUTBOUND PORTS (Interfaces) |
+------------------------------------+
• Port: Ek interface jo define karta hai ki domain logic se data kaise andar aayega ya bahar jayega (e.g.
JobSearchPort, NotificationPort).• Adapter: Concrete technology implementation (e.g.
PostgresJobSearchAdapter, TwilioSmsAdapter). Database badal kar MongoDB karne par domain code me ek single line bhi modify nahi karni padti.
3️⃣ Clean Architecture & The Dependency Rule
Uncle Bob ki Clean Architecture concentric circular layers me organize hoti hai:
| Concentric Layer | Components Included | Dependency Invariant |
|---|---|---|
| 1. Enterprise Business Rules (Entities) | Core business models (Job, Candidate, Salary, Contract). | Zero external dependencies. Pure language types only. |
| 2. Application Business Rules (Use Cases) | Use case interactors (e.g. ApplyToJobUseCase, ReviewApplicationUseCase). |
Sirf Entities layer ko janti hai. |
| 3. Interface Adapters | Controllers, Gateways, Presenters, View Models, DTOs. | Use Cases aur Entities ko external formats me translate karti hai. |
| 4. Frameworks & Drivers | Spring Boot, Express.js, PostgreSQL driver, AWS SDK, Web UI. | Outermost ephemeral layer; easily replaceable. |
4️⃣ Domain-Driven Design (DDD)
Complex business domains ko organize karne ke liye Eric Evans ka framework:
| Dimension | DDD Pattern | Definition & Application |
|---|---|---|
| Strategic Design | Bounded Context | Specific boundary jiske andar ek domain model apply hota hai (e.g. Recruitment Context me Candidate ka matlab alag hai, Payroll Context me Employee ka alag). |
| Ubiquitous Language | Single shared vocabulary jo business stakeholders aur software engineers dono identically use karte hain. | |
| Tactical Design | Entity | Unique thread of identity rakhne wala object jo time ke sath mutate ho sakta hai (e.g. JobApplication with unique UUID). |
| Value Object | Immutable object jiska koi identity nahi hota, sirf uske attributes value define karte hain (e.g. Money(amount: 50000, currency: "INR")). |
|
| Aggregate & Root | Cluster of associated entities aur value objects jise data changes ke waqt single unit treat kiya jata hai. Sirf Aggregate Root external calls accept karta hai. | |
| Domain Events | Past tense event jo domain me kuch significant hone ko represent karta hai (e.g. JobOfferAcceptedEvent). |
5️⃣ CQRS & Event Sourcing Patterns
• Command Model (Writes): State modify karta hai (Create, Update, Delete). Highly normalized, transactional, business rules aur invariants enforce karta hai.
• Query Model (Reads): State modify nahi karta. Denormalized, read-optimized views (e.g. Elasticsearch index ya Redis cache) return karta hai bina kisi complex table joins ke.
# Event Sourcing Architecture:
Traditional DB : Current State store karta hai (Overwrites rows via UPDATE).
Event Sourcing : Current state store karne ke bajay SAARI STATE CHANGING EVENTS ka immutable log persist karta hai:
1. [Event #1]: JobCreated(id=401, title="Backend Lead", salary=28)
2. [Event #2]: JobSalaryUpdated(id=401, new_salary=32)
3. [Event #3]: JobStatusChanged(id=401, status="CLOSED")
Current State = foldLeft(InitialState, EventsList) → Full Audit Trail & Time Travel Debugging!
6️⃣ Enterprise GoF Patterns in Modern Systems
| Pattern | Type | Modern Enterprise Application |
|---|---|---|
| Strategy | Behavioral | Dynamic interchangeable algorithms (e.g. Payment Gateway switching, Sorting algorithms, Cloud storage backends). |
| Factory / Abstract Factory | Creational | Complex object creation encapsulate karna bina concrete classes client code me expose kiye (Dependency Injection frameworks). |
| Decorator | Structural | Classes ko modify kiye bina dynamically naye behaviors attach karna (e.g. Logging, Caching, Rate-limiting middleware wrappers). |
| Adapter | Structural | Incompatible third-party vendor APIs ko internal enterprise interfaces me convert karna (Anti-Corruption Layer). |
7️⃣ Technical Debt, Refactoring & Anti-Patterns
• God Object (Big Ball of Mud): Single class jisme thousands of lines of code aur dozens of unrelated business operations mixed hote hain.
• Anemic Domain Model: Entities me sirf getters/setters hona aur saari business rules external service classes me leak ho jana (Violates OOP encapsulation).
• Circular Dependencies: Package A imports Package B, and Package B imports Package A (Tightly coupled nightmare; break via Dependency Inversion).
• Strangler Fig Pattern: Legacy monolith ko replace karne ka best practice: naye features ko microservices me build karna aur purane endpoints ko ek-ek karke intercept karke redirect karna jab tak legacy app completely obsolete na ho jaye.