---
title: SOLID Principles
description: Five principles of object-oriented design
type: agent/instruction
tags: [design, architecture, oop, principles]
---

{{h1}}SOLID Principles

{{h2}}S - Single Responsibility Principle

{{b}}Rule{{b}}: A class should have one, and only one, reason to change.

- Each class has single, well-defined responsibility
- Multiple responsibilities = multiple reasons to change
- Changes to one responsibility shouldn't affect others

{{h2}}O - Open/Closed Principle

{{b}}Rule{{b}}: Open for extension, closed for modification.

- Add new behaviour without changing existing code
- Extend through inheritance, composition, or interfaces
- Use strategy pattern, dependency injection, abstract classes

{{h2}}L - Liskov Substitution Principle

{{b}}Rule{{b}}: Subtypes must be substitutable for their base types.

- Subclasses must strengthen, not weaken, contracts
- Don't throw unexpected exceptions in subclass
- Don't require stronger preconditions
- Don't provide weaker postconditions
- Preserve invariants

{{h2}}I - Interface Segregation Principle

{{b}}Rule{{b}}: Clients shouldn't depend on interfaces they don't use.

- Many specific interfaces better than one general interface
- Split large interfaces into smaller, focused ones
- Clients depend only on methods they use
- Reduces coupling and side effects

{{h2}}D - Dependency Inversion Principle

{{b}}Rule{{b}}: Depend on abstractions, not concretions.

- High-level modules don't depend on low-level modules
- Both depend on abstractions
- Abstractions don't depend on details
- Use dependency injection and program to interfaces

{{h2}}Architectural Impact

- {{b}}Modularity{{b}}: Clear boundaries between components
- {{b}}Maintainability{{b}}: Localized, predictable changes
- {{b}}Testability{{b}}: Small, focused classes; mockable dependencies
- {{b}}Flexibility{{b}}: Easy extension without modification
- {{b}}Scalability{{b}}: Independent components scale separately

{{h2}}Application Guidelines

- Apply when complexity emerges, don't over-engineer early
- Balance principles based on context
- Watch for code smells: large classes (SRP), shotgun surgery (OCP), refused bequest (LSP), fat interfaces (ISP), tight coupling (DIP)
