Senior
What is SOLID? Decode the principles.
sobes.tech AI
Answer from AI
SOLID is a set of five principles of object-oriented programming aimed at creating flexible and maintainable code:
- S (Single Responsibility Principle) — The Single Responsibility Principle: a class should have only one reason to change, meaning it should perform only one task.
- O (Open/Closed Principle) — The Open/Closed Principle: software entities should be open for extension but closed for modification.
- L (Liskov Substitution Principle) — Barbara Liskov's Substitution Principle: objects of a subclass should be interchangeable with objects of the superclass without affecting the correctness of the program.
- I (Interface Segregation Principle) — The Interface Segregation Principle: it's better to have several specific interfaces than a single general-purpose interface.
- D (Dependency Inversion Principle) — The Dependency Inversion Principle: high-level modules should not depend on low-level modules; both should depend on abstractions.
Example in Go illustrating SRP and DIP:
// Single Responsibility Principle
type Logger interface {
Log(message string)
}
// Dependency Inversion Principle
func ProcessData(data string, logger Logger) {
// data processing
logger.Log("Data processed")
}