Skip to content

poojadonode28/SOLID-Principle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

SOLID-Principle

Single Responsibility Principle

Definition:
A class should have only one responsibility.


❌ Bad example (Violates SRP)

class UserService {

    void addUser(String name) {
        System.out.println("User added: " + name);
    }

    void sendEmail(String name) {
        System.out.println("Email sent to: " + name);
    }
}

✅ Good example (Follows SRP)

class UserService {

    void addUser(String name) {
        System.out.println("User added: " + name);
    }
}

class EmailService {

    void sendEmail(String name) {
        System.out.println("Email sent to: " + name);
    }
}

Open/Closed Principle (OCP)

Definition: Classes should be open for extension but closed for modification.


❌ Bad example

class DiscountCalculator {

    double calculateDiscount(String type) {
        if (type.equals("STUDENT")) {
            return 10;
        } else if (type.equals("SENIOR")) {
            return 20;
        }
        return 0;
    }
}

✅ Good example

interface Discount {
    double apply();
}

class StudentDiscount implements Discount {
    public double apply() {
        return 10;
    }
}

class SeniorDiscount implements Discount {
    public double apply() {
        return 20;
    }
}

Liskov Substitution Principle (LSP)

Definition: Objects of a superclass should be replaceable with objects of its subclasses without breaking the expected behavior.


❌ Bad example (Violates LSP)

class Vehicle {

    boolean hasEngine() {
        return true;
    }
}

class Motorcycle extends Vehicle {
    // Motorcycles have engines ✔
}

class Bicycle extends Vehicle {

    boolean hasEngine() {
        throw new UnsupportedOperationException("Bicycle has no engine");
    }
}

❌ Why this violates LSP

Vehicle promises that hasEngine() works

Bicycle breaks that contract

Code using Vehicle will crash when replaced with Bicycle

Example of breakage:

Vehicle vehicle = new Bicycle();
vehicle.hasEngine(); // 💥 Runtime exception

✅ Good example (Follows LSP)

interface Vehicle {
}

interface EngineVehicle extends Vehicle {
    boolean hasEngine();
}

class Motorcycle implements EngineVehicle {

    public boolean hasEngine() {
        return true;
    }
}

class Bicycle implements Vehicle {
    // No engine-related behavior
}

Interface Segregation Principle (ISP)

Definition: A class should not be forced to implement methods it does not use.

❌ Bad example (Violates ISP)

interface Worker {
    void work();
    void eat();
}

class HumanWorker implements Worker {

    public void work() {
        System.out.println("Human working");
    }

    public void eat() {
        System.out.println("Human eating");
    }
}

class RobotWorker implements Worker {

    public void work() {
        System.out.println("Robot working");
    }

    public void eat() {
        // ❌ Robots don’t eat
        throw new UnsupportedOperationException("Robots do not eat");
    }
}

✅ Good example (Follows ISP)

interface Workable {
    void work();
}

interface Eatable {
    void eat();
}

class HumanWorker implements Workable, Eatable {

    public void work() {
        System.out.println("Human working");
    }

    public void eat() {
        System.out.println("Human eating");
    }
}

class RobotWorker implements Workable {

    public void work() {
        System.out.println("Robot working");
    }
}

Dependency Inversion Principle (DIP)

Definition: Classes should depend on interfaces not on concrete classes.


❌ Without DIP (Bad)

class MessageService {

    void send() {
        System.out.println("Sending message");
    }
}

class Notification {

    private MessageService messageService = new MessageService();

    void notifyUser() {
        messageService.send();
    }
}

✅ With DIP (Good)

interface MessageService {
    void send();
}

class EmailService implements MessageService {

    public void send() {
        System.out.println("Sending email");
    }
}

class Notification {

    private MessageService messageService;

    Notification(MessageService messageService) {
        this.messageService = messageService;
    }

    void notifyUser() {
        messageService.send();
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published