SOLID Principles in Java - Violations & Solutions
1. Single Responsibility Principle (SRP)
Violation:
class UserManager {
public void createUser(String name) {
[Link]("Creating user: " + name);
}
public void sendEmail(String email) {
[Link]("Sending email to: " + email);
}
}
Solution:
class UserCreator {
public void createUser(String name) {
[Link]("Creating user: " + name);
}
}
class EmailSender {
public void sendEmail(String email) {
[Link]("Sending email to: " + email);
}
}
---
2. Open/Closed Principle (OCP)
Violation:
// If AreaCalculator handled each shape specifically, it would need modification for new
shapes
Solution:
abstract class Shape {
public abstract double area();
}
class Rectangle extends Shape {
private double width, height;
public Rectangle(double width, double height) { [Link] = width; [Link] =
height; }
@Override public double area() { return width * height; }
}
class Circle extends Shape {
private double radius;
public Circle(double radius) { [Link] = radius; }
@Override public double area() { return 3.14 * radius * radius; }
}
class AreaCalculator {
public double calculateArea(Shape shape) {
return [Link]();
}
}
---
3. Liskov Substitution Principle (LSP)
Violation:
class Bird {
public void fly() { [Link]("Flying"); }
}
class Penguin extends Bird {
@Override public void fly() { throw new UnsupportedOperationException("I can't
fly!"); }
}
Solution:
abstract class Bird { }
interface Flyable { void fly(); }
class Sparrow extends Bird implements Flyable {
public void fly() { [Link]("Sparrow flying"); }
}
class Penguin extends Bird { }
---
4. Interface Segregation Principle (ISP)
Violation:
interface Worker {
void work();
void eat();
}
class Robot implements Worker {
public void work() { [Link]("Robot working"); }
public void eat() { throw new UnsupportedOperationException(); }
}
Solution:
interface Workable { void work(); }
interface Eatable { void eat(); }
class Human implements Workable, Eatable {
public void work() { [Link]("Human working"); }
public void eat() { [Link]("Human eating"); }
}
class Robot implements Workable {
public void work() { [Link]("Robot working"); }
}
---
5. Dependency Inversion Principle (DIP)
Violation:
class MySQLDatabase {
public String getData() { return "data"; }
}
class DataProcessor {
private MySQLDatabase db = new MySQLDatabase();
public void process() {
[Link]([Link]());
}
}
Solution:
interface Database {
String getData();
}
class MySQLDatabase implements Database {
public String getData() { return "data"; }
}
class DataProcessor {
private Database db;
public DataProcessor(Database db) { [Link] = db; }
public void process() {
[Link]([Link]());
}
}
public class Main {
public static void main(String[] args) {
Database db = new MySQLDatabase();
DataProcessor processor = new DataProcessor(db);
[Link]();
}
}
---
Additional DIP Example (Keyboard):
abstract class Keyboard {
public abstract void type();
}
class MechanicalKeyboard extends Keyboard {
@Override public void type() { [Link]("Typing with Mechanical
Keyboard"); }
}
class WirelessKeyboard extends Keyboard {
@Override public void type() { [Link]("Typing with Wireless Keyboard");
}
}
class Computer {
private Keyboard keyboard;
public Computer(Keyboard keyboard) { [Link] = keyboard; }
public void useKeyboard() { [Link](); }
}
public class Main {
public static void main(String[] args) {
Keyboard mech = new MechanicalKeyboard();
Computer pc = new Computer(mech);
[Link]();
}
}