Solid Programming
Solid Programming
import java.io.FileWriter;
import java.io.IOException;
class User {
private String name;
private String email;
class UserPersistence {
public static void saveToFile(UserGood user) {
try (FileWriter writer = new FileWriter(user.getName() + ".txt")) {
writer.write("Name: " + user.getName() + "\nEmail: " + user.getEmail());
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Circle {
public int radius;
public Circle(int radius) {
this.radius = radius;
}
}
class AreaCalculator {
public double calculateArea(Object shape) {
if (shape instanceof Rectangle r) {
return r.width * r.height;
} else if (shape instanceof Circle c) {
return 3.14 * c.radius * c.radius;
}
return 0;
}
}
class AreaCalculatorGood {
public double calculateArea(Shape shape) {
return shape.area();
}
}
class TestLSP {
public static void makeBirdFly(Bird bird) {
bird.fly();
}
interface FlyingBird {
void fly();
}
interface Eatable {
void eat();
}
class PasswordReminder {
private MySQLDatabase db;
public PasswordReminder() {
this.db = new MySQLDatabase();
}
class PasswordReminderGood {
private Database db;