Sitemap
SelimCelik

SelimCelik

Game Developer

SOLID Principles

2 min readJul 19, 2024

--

Press enter or click to view image in full size

In software design, knowing and applying the SOLID principles is essential for ensuring a proper structure and sustainability. In this article, you will learn about the SOLID principles and how you can improve your software development process by explaining each one.

Section 1: Single Responsibility Principle (SRP)

The Single Responsibility Principle states that a class should have only one job. This is key to keeping classes small and focused.

public class FileManager
{
public void SaveFile(string fileName, string content)
{
// Saving file operation
}

public void LoadFile(string fileName)
{
// Loading file operation
}
}

Section 2: Open/Closed Principle (OCP)

The Open/Closed Principle states that a class should be open for extension but closed for modification.

public abstract class Shape
{
public abstract double Area();
}

public class Circle : Shape
{
public double Radius { get; set; }

public override double Area()
{
return Math.PI * Radius * Radius;
}
}

Get SelimCelik’s stories in your inbox

Join Medium for free to get updates from this writer.

Section 3: Liskov Substitution Principle (LSP)

The Liskov Substitution Principle states that derived classes must be substitutable for their base classes.

public class Bird
{
public void Fly()
{
// Flying operation
}
}

public class Sparrow : Bird
{
// Sparrow can be used in place of Bird
}

Section 4: Interface Segregation Principle (ISP)

The Interface Segregation Principle states that a client should not be forced to depend on interfaces it does not use.

public interface IWorker
{
void Work();
}

public interface IEater
{
void Eat();
}

public class Robot : IWorker
{
public void Work()
{
// Working operation
}
}

Section 5: Dependency Inversion Principle (DIP)

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules. Both should depend on abstractions.

public interface IDataProvider
{
string GetData();
}

public class DatabaseDataProvider : IDataProvider
{
public string GetData()
{
// Retrieving data from the database
}
}

Conclusion

In this article, you learned about the SOLID principles, which are the cornerstones of software design. We provided examples to explain each principle and showed how you can apply them. By using the SOLID principles in your projects, you can make your code more readable, sustainable, and flexible.

--

--