Solid Principles PDF
Solid Principles PDF
SOLID
S=> Single Responsibility Principle
O=>Open-Closed Principle
L=>Liskov Substitution Principle
I=> Interface Segregation Principle
D=> Dependency Injection
SINGLE RESPONSIBILITY PRINCIPLE
Described by Rober Martin “A Class should have only one reason to change”
Meaning: Class/ Method should be responsible for doing only one thing
EXAMPLE
State that “class should be open for extension, but closed for modification”
We need to design system in such way that it can adapt the features without modifying, and deploying core
component
EXAMPLE
public class Account{ public interface Interest{
string AccType; public double CalculateInterest(double bal);
}
public double CalculateInterest(double bal) public class SavingAccount : Account {
{ public double CalculateInterest(double bal)
if(AccType==“Saving”) { return bal*0.6;
{ }
return bal*0.6; }
}
public class CurrentAccount : Account {
else if(AccType==“Current”)
{ public double CalculateInterest(double bal)
return bal*0.2; { return bal*0.2;
} }
else }
{
return 0;
//Access Interest
}
public class Account {
}
public double CalculateInterest(Interest itr)
}
{ itr.CalculateInterest(double);
}
If we want to change interest calculation then }
Account class needs to be modified
LISKOV SUBSTITUTION PRINCIPLE
State that “If for each object o2 of type T such that for all programs P defined in terms of T, the behavior or P is
unchanged when o1 is substituted for o2 then S is a subtype of T.”
In other word derived types should be substitutable for their base type ie. Implementation is substituted
EXAMPLE
public class Animal public class Dog : Animal
{ { Implementation:
public string Walk()
public override string MakeNoise()
{
return "Move feet"; {
} return "Bark";
static void Main(string[] args)
}
public string Run() } {
{ Animal animal = new Dog();
return "Move feet quickly"; public class Bird: Animal Console.WriteLine(animal.Walk());
}
{ Console.WriteLine(animal.Run());
public virtual string Fly() public override string MakeNoise() Console.WriteLine(animal.Fly());
{ {
return null; return "Chirp";
}
Console.WriteLine(animal.MakeNoise());
}
Console.ReadLine();
public virtual string MakeNoise()
public override string Fly() }
{
return null; {
} return "Flag wings";
} }
}
INTERFACE SEGREGATION
State that “No client should be forced to depend on methods it doesn’t use”
In other words, each class should be served for specific purpose and class shouldn’t be forced to depend on
methods which they do not require
Its related with Single responsibility principle
EXAMPLE
Problem:
1)If we implement interface on ProductA and ProductB class
Interface Iproduct{ then we need to implement all methods declared in Iproduct
void CreateProduct(); interface
void CreateProductEmbroidery();
void getProductPrice(); 2) ProductA = which specify normal product which doesn’t
void getProductPricewithEmb() have embroidery
} productB= which specify product with embroidery which has
//for normal product
different price calculation
public class ProductA : Iproduct 3) In this scenario ProductA should implement only
{ CreateProduct and GetProductPrice() and rest will be
} implement in ProductB, Thus it violate the ISP
//for product with embroidery
public class ProductB : Iproduct
{
}
CONTINUE…
State that “ A High Level Modules should not depend on low level modules, both should depends on abstraction.
Abstraction should not depends on details. Details should depends on abstraction”
In other words Its technique that makes a class independent of its dependencies.
decoupling the usage of an object from its creation
EXAMPLE
public class Report {
private IReport report;
Report (IReport reportobj)
public class Report {
{
PDFReport p=new PDFReport(); report = reportobj;
void printReport() }
{ void printReport()
p.print(); {
} report.print()
}
}
}