SOLID Design Principles
SOLID Design Principles
Open/Closed Principle
1. “Software entities should be open for extension, but closed for modification”
2. The design and writing of the code should be done in a way that new functionality should be
added with minimum changes in the existing code
3. The design should be done in a way to allow the adding of new functionality as new classes,
keeping as much as possible existing code unchanged
SOLID Acronym
S : Single Responsibility Principle
O : Open closed Principle
L : Liskov substitution Principle
I : Interface Segregation Principle (ISP)
D : Dependency Inversion Principle (DIP)
namespace SRPDemo
{
interface IUser
{
bool Login(string username, string password);
bool Register(string username, string password, string email);
void LogError(string error);
bool SendEmail(string emailContent);
}
}
namespace SRPDemo
{
interface IUser
{
bool Login(string username, string password);
bool Register(string username, string password, string email);
}
interface ILogger
{
void LogError(string error);
}
interface IEmail
{
bool SendEmail(string emailContent);
}
}
Open/Close Principle
namespace OpenClosedDemo
{
public abstract class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public Employee()
{
}
public Employee(int id, string name )
{
this.ID = id; this.Name = name;
}
public abstract decimal CalculateBonus(decimal salary);
namespace OpenClosedDemo
{
class Program
{
static void Main(string[] args)
{
Employee empJohn = new PermanentEmployee(1, "John" );
Employee empJason = new TemporaryEmployee(2, "Jason" );
namespace SOLID_PRINCIPLES.LSP
{
class Program
{
static void Main(string[] args)
{
Fruit fruit = new Orange();
Console.WriteLine(fruit.GetColor());
fruit = new Apple();
Console.WriteLine(fruit.GetColor());
}
}
public abstract class Fruit
{
public abstract string GetColor();
}
public class Apple : Fruit
{
public override string GetColor()
{
return "Red";
}
}
public class Orange : Fruit
{
public override string GetColor()
{
return "Orange";
}
}
}