Download - Solid principles - Object oriendte
![Page 1: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/1.jpg)
SOLID PrinciplesDon't dare to violate them!!
![Page 2: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/2.jpg)
Single Responsibility Principle
A Class should have one and only one
reason to change
![Page 3: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/3.jpg)
public class Task{
public void downloadFile(String location)
public void parseTheFile(File file)
public void persistTheData(Data data)}
Single Responsibility Principle - Example 1
![Page 4: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/4.jpg)
Single Responsibility Principle - Example 2
public class Employee { public Money calculatePay() ... public String reportHours() ... public void save() ...}
![Page 5: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/5.jpg)
Single Responsibility Principle - for methods
● Method should do related things● A method should be at a high level or a low
level
![Page 6: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/6.jpg)
Open/Closed Principle (OCP)
Software entities should be
open for extensionbut
closed for modification
![Page 7: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/7.jpg)
Open/Closed Principle (OCP) - Example 1
class AreaCalculator
public double Area(Rectangle[] shapes) double area = 0; foreach (var shape in shapes)
area += shape.Width*shape.Height;return area;
![Page 8: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/8.jpg)
Open/Closed Principle (OCP) - Example 1 - Add Circlepublic double Area(object[] shapes)
double area = 0;
foreach (var shape in shapes) if (shape is Rectangle) {
Rectangle rectangle = (Rectangle) shape; area += rectangle.Width*rectangle.Height;
} else { Circle circle = (Circle)shape; area += circle.Radius * circle.Radius * Math.PI;
}return area;
![Page 9: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/9.jpg)
Open/Closed Principle (OCP) - Example 1 - Solutionpublic abstract class Shape{
public abstract double Area();}
public class Rectangle : Shape{
public double Width { get; set; }public double Height { get; set; }public override double Area(){
return Width*Height;}
}
![Page 10: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/10.jpg)
Open/Closed Principle (OCP) - Example 1 - Solutionpublic class Circle : Shape{
public double Radius { get; set; }public override double Area(){
return Radius*Radius*Math.PI;}
}public double Area(Shape[] shapes){
double area = 0;foreach (var shape in shapes){
area += shape.Area();}return area;
}
![Page 11: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/11.jpg)
Liskov substitution principle (LSP)
Subtypes must be substitutable
for their base types.
![Page 12: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/12.jpg)
Liskov substitution principle (LSP)
A subclass may override a parent method only under certain conditions
● Preconditions can only be weaker.● Postconditions can only be stronger.
![Page 13: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/13.jpg)
class Rectangle{ void setWidth(double w) void setHeight(double h) double getHeight() double getWidth()}class Square{ void setWidth(double w) //Set both height and width to w void setHeight(double h) //Set height and width values to h double getHeight() double getWidth()}
Liskov substitution principle (LSP) - Example 1
![Page 14: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/14.jpg)
void test(Rectangle r){ r.setWidth(5); r.setHeight(4); assertEquals(5 * 4, r.setWidth() * r.setHeight());}
Liskov substitution principle (LSP) - Example 1
![Page 15: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/15.jpg)
Interface Segregation Principle (ISP)
The dependency of one class to another one should depend on the smallest possible interface.
● Clients should not be forced to implement interfaces they don't use.
● Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submodule.
![Page 16: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/16.jpg)
Animal void feed(); //abstract
Dog implements Animal void feed() //implementation
Tiger implements Animalvoid feed() //implementation
Interface Segregation Principle (ISP) - Example 1
![Page 17: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/17.jpg)
Animal void feed(); //abstract void groom(); //abstract
Dog extends Animal void feed() //implementation void groom(); //implementation
Tiger extends Animalvoid feed() //implementationvoid groom(); //DUMMY implementation - to keep compiler
happy
Interface Segregation Principle (ISP) - Example 1 - Enhanced to groom
![Page 18: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/18.jpg)
Animal void feed(); //abstract
Pet extends Animal void groom(); //abstract
Dog extends Pet void feed() //implementation void groom(); //implementation
Tiger extends Animalvoid feed() //implementation
Interface Segregation Principle (ISP) - Example 1 - Better solution
![Page 19: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/19.jpg)
Dependency Inversion Principle (DIP)
Depend upon abstractions (interfaces)
not upon concrete classes.
![Page 20: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/20.jpg)
enum OutputDevice {printer, disk};void copy(OutputDevice dev){ int c; while ((c = ReadKeyboard()) != EOF) { if (dev == printer) writePrinter(c); else writeDisk(c); }}
Dependency Inversion Principle (DIP) - Example 1
![Page 21: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/21.jpg)
interface Reader char read();
interface Writer char write(char ch);
void copy(Reader r, Writer w){ char ch; while((ch = r.read())!=EOF) { w.write(ch); }}
Dependency Inversion Principle (DIP) - Example 1
![Page 22: Solid principles - Object oriendte](https://reader033.vdocuments.mx/reader033/viewer/2022042619/5872a05d1a28ab07208b5945/html5/thumbnails/22.jpg)
THANK YOU