├── .idea ├── .gitignore ├── vcs.xml ├── modules.xml └── misc.xml ├── src └── com │ └── amigoscode │ ├── Shape.java │ ├── ThreeDimensionalShape.java │ ├── Rectangle.java │ ├── NoShape.java │ ├── Cube.java │ ├── AreaCalculator.java │ ├── AreaCalculatorV2.java │ ├── Square.java │ ├── Circle.java │ ├── ShapesPrinter.java │ ├── solid.txt │ ├── IAreaCalculator.java │ └── Main.java ├── solid.iml └── README.md /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /src/com/amigoscode/Shape.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public interface Shape { 4 | double area(); 5 | } 6 | -------------------------------------------------------------------------------- /src/com/amigoscode/ThreeDimensionalShape.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public interface ThreeDimensionalShape { 4 | double volume(); 5 | } 6 | -------------------------------------------------------------------------------- /src/com/amigoscode/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public class Rectangle implements Shape { 4 | @Override 5 | public double area() { 6 | return 20; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/com/amigoscode/NoShape.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public class NoShape implements Shape { 4 | @Override 5 | public double area() { 6 | throw new IllegalStateException( 7 | "Cannot calculate area"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/com/amigoscode/Cube.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public class Cube implements Shape, ThreeDimensionalShape { 4 | @Override 5 | public double area() { 6 | return 100; 7 | } 8 | 9 | @Override 10 | public double volume() { 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/com/amigoscode/AreaCalculator.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | import java.util.List; 4 | 5 | public class AreaCalculator implements IAreaCalculator { 6 | 7 | @Override 8 | public int sum(List shapes) { 9 | int sum = 0; 10 | for (int i = 0; i < shapes.size(); i++) { 11 | sum += shapes.get(i).area(); 12 | } 13 | return sum; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/com/amigoscode/AreaCalculatorV2.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | import java.util.List; 4 | 5 | public class AreaCalculatorV2 implements IAreaCalculator { 6 | 7 | @Override 8 | public int sum(List shapes) { 9 | int sum = 0; 10 | for (int i = 0; i < shapes.size(); i++) { 11 | sum += shapes.get(i).area(); 12 | } 13 | return sum; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/com/amigoscode/Square.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public class Square implements Shape { 4 | private final int length; 5 | 6 | public Square(int length) { 7 | this.length = length; 8 | } 9 | 10 | public int getLength() { 11 | return length; 12 | } 13 | 14 | @Override 15 | public double area() { 16 | return Math.pow(getLength(), 2); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /src/com/amigoscode/Circle.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | public class Circle implements Shape { 4 | private final int radius; 5 | 6 | public Circle(int radius) { 7 | this.radius = radius; 8 | } 9 | 10 | public int getRadius() { 11 | return radius; 12 | } 13 | 14 | @Override 15 | public double area() { 16 | return Math.PI * Math.pow(getRadius(), 2); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /solid.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/com/amigoscode/ShapesPrinter.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | import java.util.List; 4 | 5 | public class ShapesPrinter { 6 | 7 | private final IAreaCalculator areaCalculator; 8 | 9 | public ShapesPrinter(IAreaCalculator areaCalculator) { 10 | this.areaCalculator = areaCalculator; 11 | } 12 | 13 | public String json(List shapes) { 14 | return "{shapesSum: %s}".formatted( 15 | areaCalculator.sum(shapes)); 16 | } 17 | 18 | public String csv(List shapes) { 19 | return "shapes_sum,%s".formatted( 20 | areaCalculator.sum(shapes)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/com/amigoscode/solid.txt: -------------------------------------------------------------------------------- 1 | SOLID PRINCIPLES 2 | 3 | ➡️ Single Responsibility 4 | Each class should have only one sole purpose, 5 | and not be filled with excessive functionality 6 | 7 | ➡️ Open Closed 8 | Classes should be open for extension, 9 | closed for modification. 10 | 11 | In other words, you should not have to rewrite 12 | an existing class for implementing new features. 13 | 14 | ➡️ Liskov Substitution 15 | This means that every subclass or 16 | derived class should be substitutable for their 17 | base or parent class 18 | 19 | ➡️ Interface Segregation 20 | Interfaces should not force classes to implement 21 | what they can’t do. 22 | Large interfaces should be divided into small ones. 23 | 24 | ➡️ Dependency Inversion 25 | Components should depend on abstractions, 26 | not on concretions. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SOLID Principles 2 | 3 | ## YT Video 4 | https://amigoscode.hypg.es/solid-youtube 5 | ![solid](https://user-images.githubusercontent.com/40702606/164469254-1763ca82-0b20-4bbb-b250-e0389660b2e0.png) 6 | 7 | ## SOLID 8 | 9 | - Single Responsibility 10 | Each class should have only one sole purpose, and not be filled with excessive functionality 11 | 12 | - Open Closed 13 | Classes should be open for extension, closed for modification. 14 | In other words, you should not have to rewrite an existing class for implementing new features. 15 | 16 | - Liskov Substitution 17 | Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T. 18 | This means that every subclass or derived class should be substitutable for their base or parent class 19 | 20 | - Interface Segregation 21 | Interfaces should not force classes to implement what they can’t do. 22 | Large interfaces should be divided into small ones. 23 | 24 | - Dependency Inversion 25 | Components should depend on abstractions, not on concretions. 26 | -------------------------------------------------------------------------------- /src/com/amigoscode/IAreaCalculator.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | import java.util.List; 4 | 5 | public interface IAreaCalculator { 6 | int sum(List shapes); 7 | 8 | class Main { 9 | public static void main(String[] args) { 10 | IAreaCalculator areaCalculator = new AreaCalculator(); 11 | IAreaCalculator areaCalculatorV2 = new AreaCalculatorV2(); 12 | Circle circle = new Circle(10); 13 | Square square = new Square(10); 14 | Cube cube = new Cube(); 15 | Rectangle rectangle = new Rectangle(); 16 | ShapesPrinter printer = new ShapesPrinter( 17 | areaCalculatorV2); 18 | Shape noShape = new NoShape(); 19 | List shapes = List.of( 20 | circle, 21 | square, 22 | cube, 23 | rectangle); 24 | int sum = areaCalculator.sum(shapes); 25 | System.out.println(printer.json(shapes)); 26 | System.out.println(printer.csv(shapes)); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/amigoscode/Main.java: -------------------------------------------------------------------------------- 1 | package com.amigoscode; 2 | 3 | import java.io.*; 4 | import java.lang.reflect.Field; 5 | import java.util.Scanner; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | // Working with Files 10 | File file = createFile("src/foo.txt"); 11 | 12 | writeToFile(file, true); 13 | 14 | try { 15 | Scanner scanner = new Scanner(file); 16 | while (scanner.hasNext()) { 17 | System.out.println(scanner.nextLine()); 18 | } 19 | } catch (FileNotFoundException e) { 20 | System.out.println(e.getMessage()); 21 | } 22 | } 23 | 24 | private static void writeToFile(File file, boolean append) { 25 | try ( 26 | FileWriter fileWriter = new FileWriter(file, append); 27 | PrintWriter writer = new PrintWriter(fileWriter); 28 | ) { 29 | writer.println("Bianca"); 30 | } catch (IOException e) { 31 | System.out.println(e.getMessage()); 32 | } 33 | 34 | // try { 35 | // FileWriter fileWriter = new FileWriter(file, append); 36 | // PrintWriter writer = new PrintWriter(fileWriter); 37 | // writer.println("Jamila"); 38 | // writer.flush(); 39 | // writer.close(); 40 | // } catch (IOException e) { 41 | // System.out.println(e.getMessage()); 42 | // } 43 | } 44 | 45 | private static File createFile(String path) { 46 | try { 47 | File file = new File(path); 48 | if (!file.exists()) { 49 | file.createNewFile(); 50 | } 51 | return file; 52 | } catch (IOException e) { 53 | System.out.println(e.getMessage()); 54 | throw new IllegalStateException(e); 55 | } 56 | } 57 | } 58 | 59 | --------------------------------------------------------------------------------