SOLID Flashcards
(6 cards)
What does SOLID stand for in software development?
SOLID is an acronym for five object-oriented design principles that help create maintainable, scalable, and flexible software. The principles are:
1. S – Single Responsibility Principle (SRP)
2. O – Open/Closed Principle (OCP)
3. L – Liskov Substitution Principle (LSP)
4. I – Interface Segregation Principle (ISP)
5. D – Dependency Inversion Principle (DIP)
These principles help developers write clean, modular, and loosely coupled code in languages like C# and Java.
What is the S in SOLID, and what does it mean?
S stands for Single Responsibility Principle (SRP). It means that a class should have only one reason to change, meaning it should have one responsibility.
Example in C#:
public class Invoice
{
public void CalculateTotal() { /* Logic */ }
}
public class InvoicePrinter
{
public void Print(Invoice invoice) { /* Printing logic */ }
}
Here, Invoice is responsible for calculations, and InvoicePrinter handles printing.
What is the O in SOLID, and what does it mean?
O stands for Open/Closed Principle (OCP). It means that a class should be open for extension but closed for modification, allowing behavior to be added without changing existing code.
Example in C#:
public abstract class Shape
{
public abstract double Area();
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double Area() => Math.PI * Radius * Radius;
}
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override double Area() => Width * Height;
}
Here, new shapes can be added without modifying existing code.
What is the L in SOLID, and what does it mean?
L stands for Liskov Substitution Principle (LSP). It means that subtypes must be substitutable for their base types without breaking functionality.
Example in C#:
public class Bird
{
public virtual void Fly() { Console.WriteLine(“Flying…”); }
}
public class Sparrow : Bird { }
public class Penguin : Bird
{
public override void Fly() { throw new NotImplementedException(); }
}
Here, Penguin violates LSP because it cannot fly, breaking expectations of the Bird base class.
✅ Fix: Introduce a more appropriate hierarchy:
public abstract class Bird { }
public class FlyingBird : Bird { public void Fly() { /* Flying logic */ } }
public class Penguin : Bird { }
What is the I in SOLID, and what does it mean?
I stands for Interface Segregation Principle (ISP). It means that a class should not be forced to implement interfaces it does not use.
Example in C#:
public interface IWorker
{
void Work();
void Eat();
}
public class HumanWorker : IWorker
{
public void Work() { /* Work logic / }
public void Eat() { / Eating logic */ }
}
public class RobotWorker : IWorker
{
public void Work() { /* Work logic */ }
public void Eat() { throw new NotImplementedException(); } // ❌ Violates ISP
}
✅ Fix: Split interfaces:
public interface IWorkable { void Work(); }
public interface IEatable { void Eat(); }
public class HumanWorker : IWorkable, IEatable
{
public void Work() { /* Work logic / }
public void Eat() { / Eating logic */ }
}
public class RobotWorker : IWorkable
{
public void Work() { /* Work logic */ }
}
What is the D in SOLID, and what does it mean?
D stands for Dependency Inversion Principle (DIP). It states that high-level modules should not depend on low-level modules; both should depend on abstractions.
Example in C#:
// Bad Example: High-level module depends on low-level module
public class FileLogger
{
public void Log(string message) { Console.WriteLine(message); }
}
public class UserService
{
private FileLogger _logger = new FileLogger();
public void RegisterUser(string username)
{
_logger.Log(“User registered: “ + username);
}
}
✅ Fix: Use Dependency Injection with an abstraction:
public interface ILogger
{
void Log(string message);
}
public class FileLogger : ILogger
{
public void Log(string message) { Console.WriteLine(message); }
}
public class UserService
{
private readonly ILogger _logger;
public UserService(ILogger logger)
{
_logger = logger;
}
public void RegisterUser(string username) { _logger.Log("User registered: " + username); } }
Now, UserService can work with any logger implementation (e.g., DatabaseLogger), making it flexible.