Open/Closed Prensibi Nedir ? (Kod örneğiyle) — SOLID

Open/Closed Yazılım Prensibi
public class Rectangle { private double length; 
private double height;
// getters/setters ...
}
public class AreaService {

public double calculateArea(List<Rectangle>... shapes) {
double area = 0;
for (Rectangle rect : shapes) {
area += (rect.getLength() * rect.getHeight());
}
return area;
}
}
public class Circle { 
private double radius;
// getters/setters …
}
public class AreaService {
public double calculateArea(List<Object>... shapes) {
double area = 0;
for (Object shape : shapes) {
if (shape instanceof Rectangle) {
Rectangle rect = (Rectangle) shape;
area += (rect.getLength() * rect.getHeight());
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
area += circle.getRadius() * cirlce.getRadius() * Math.PI;
} else {
throw new RuntimeException("Shape not supported");
}
} return area;
}
}
public interface Shape { 
double getArea();
}
public class Rectangle implements Shape {
private double length;
private double height;

// getters/setters …
@Override
public double getArea() {
return (length * height);
}
}
public class Circle implements Shape {
private double radius;

// getters/setters …
@Override
public double getArea() {
return (radius * radius * Math.PI);
}
}
public class AreaManager {
public double calculateArea(List<Shape> shapes) {
double area = 0;
for (Shape shape : shapes) {
area += shape.getArea();
}
return area;
}
}

--

--

Software Engineer @Yemeksepeti • #Java • #Spring Boot • #Kotlin • #Spark • #Microservices • https://gokhana.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gökhan Ayrancıoğlu

Gökhan Ayrancıoğlu

Software Engineer @Yemeksepeti • #Java • #Spring Boot • #Kotlin • #Spark • #Microservices • https://gokhana.dev