Jump to content

SOLID (object-oriented design)

From Simple English Wikipedia, the free encyclopedia

SOLID is a mnemonic acronym for a group of five good principles (rules) in computer programming. SOLID allows programmers to write code that is easier to understand and change later on. SOLID is often used with systems that use an object-oriented design. [source?]

SOLID was promoted by Robert C. Martin but the name itself was created by Michael Feathers. [1][better source needed]

In essence, SOLID principles guide developers towards creating code that is more maintainable, flexible, and reusable. By adhering to these principles, one can build software systems that are easier to understand, modify, and extend over time. [source?]

SOLID Principles

[change | change source]

Single responsibility principle (SRP)

[change | change source]

Class has one job to do. Each change in requirements can be done by changing just one class.[source?]

Importance

[change | change source]
  • Maintainability: When classes have a single, well-defined responsibility, they're easier to understand and modify.[source?]
  • Testability: It's easier to write unit tests for classes with a single focus.[source?]
  • Flexibility: Changes to one responsibility don't affect unrelated parts of the system.[source?]

Open/closed principle (OCP)

[change | change source]

Class is happy (open) to be used by others. Class is not happy (closed) to be changed by others.[source?]

Importance

[change | change source]
  • Extensibility: New features can be added without modifying existing code.[source?]
  • Stability: Reduces the risk of introducing bugs when making changes.[source?]
  • Flexibility: Adapts to changing requirements more easily.[source?]

Liskov substitution principle (LSP)

[change | change source]

Class can be replaced by any of its children. Children classes inherit parent's behaviours.[source?]

Importance

[change | change source]
  • Polymorphism: Enables the use of polymorphic behavior, making code more flexible and reusable.[source?]
  • Reliability: Ensures that subclasses adhere to the contract defined by the superclass.[source?]
  • Predictability: Guarantees that replacing a superclass object with a subclass object won't break the program.[source?]

Interface segregation principle (ISP)

[change | change source]

When classes promise each other something, they should separate these promises (interfaces) into many small promises, so it's easier to understand.[source?]

Importance

[change | change source]
  • Decoupling: Reduces dependencies between classes, making the code more modular and maintainable.[source?]
  • Flexibility: Allows for more targeted implementations of interfaces.[source?]
  • Avoids unnecessary dependencies: Clients don't have to depend on methods they don't use.[source?]

Dependency inversion principle (DIP)

[change | change source]

When classes talk to each other in a very specific way, they both depend on each other to never change. Instead classes should use promises (interfaces, parents), so classes can change as long as they keep the promise.[source?]

Importance

[change | change source]
  • Loose coupling: Reduces dependencies between modules, making the code more flexible and easier to test.[source?]
  • Flexibility: Enables changes to implementations without affecting clients.[source?]
  • Maintainability: Makes code easier to understand and modify.[source?]
[change | change source]

References

[change | change source]
  1. "Uncle Bob Martin on Clean Software, Craftsperson, Origins of SOLID, DDD, & Software Ethics". InfoQ. 24 August 2018. Archived from the original on 24 August 2018.