0% found this document useful (0 votes)
137 views14 pages

Solid Principles PDF

The document discusses several SOLID principles of object-oriented design, including the single responsibility principle, open-closed principle, Liskov substitution principle, interface segregation principle, and dependency injection principle. Examples are provided to illustrate how to apply each principle.

Uploaded by

shivaji.kore103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views14 pages

Solid Principles PDF

The document discusses several SOLID principles of object-oriented design, including the single responsibility principle, open-closed principle, Liskov substitution principle, interface segregation principle, and dependency injection principle. Examples are provided to illustrate how to apply each principle.

Uploaded by

shivaji.kore103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SOLID PRINCIPLES

Created By – Kashinath Patil


7 Nov 2019
ACRONYMS

 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

public class Invoice{


Public class Invoice{ double CalculateTotal(){
double CalculateTotal(){ }
} }
interface StoreInvoice{
void SaveInvoiceToDb(){
void SaveInvoiceToDb(){}
}
}

void ExportInvoice(){ interface StoreInvoice{


} void ExportInvoice()
}
}
OPEN-CLOSED PRINCIPLE

 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…

Interface IproductA{ Interface IproductB{


void CreateProduct(); void CreateProductEmbroidery();
void getProductPrice(); void getProductPricewithEmb()
} }

//for normal product //for product with embroidery


public class ProductA : IproductA public class ProductB : IproductB
{ {
} }
DEPENDENCY INJECTION

 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()
}
}
}

public class PDFReport { Interface IReport{


void print() void print();
{ }
//code public class PDFReport : IReport{
void print()
}
{
} //code
}
}
Here Report class has dependency on specific public class ExcelReport : IReport{
PDFReport class; but consider if we want to modify or void print()
{
add some different report then this code will violates
//code
DI principle }
}
Thank You

You might also like