Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Refactoring #9

Open
nfiallo5 opened this issue Nov 12, 2024 · 0 comments
Open

Code Refactoring #9

nfiallo5 opened this issue Nov 12, 2024 · 0 comments

Comments

@nfiallo5
Copy link

Hello madhuv-sharma

I identified some violations of the SOLID principles in the current implementation of the Laundry, Transportation, and Service classes. To improve code quality and flexibility, I propose a refactoring using new classes and interfaces. Below, I outline the issues found and the implemented solution:

Identified Issues:
Single Responsibility Principle (SRP) Violation:
The Laundry and Transportation classes are handling multiple responsibilities (input handling, cost logic, and detail storage), which goes against the SRP.

Open/Closed Principle (OCP) Violation:
The setDetails() methods in Laundry and Transportation depend on multiple cases, making it difficult to extend functionality without modifying the base code.

Interface Segregation Principle (ISP) Violation:
The Service class includes methods that aren’t necessary for all service types, such as getTotalCost and getStatus.

Solutions that I came with:
Modularization through Strategies and Interfaces:
I created interfaces (Costeable, Statuseable, Detallable) and strategy classes (BasicWashStrategy, DeluxeWashStrategy, PremiumWashStrategy) to manage different wash cost types. This approach separates input handling and cost logic into independent classes.

Dependency Inversion Principle (DIP) Application:
Classes now depend on abstractions (interfaces) rather than concrete implementations, making it easier to extend service types without modifying existing classes.

I've attached some of the refactored code with the improvements incorporated:

public class Laundry implements Costeable, Statuseable, Detallable {
	private CostStrategy costStrategy;
	private int quantity;
	private boolean status;
 
	public Laundry(CostStrategy costStrategy, int quantity) {
	    this.costStrategy = costStrategy;
	    this.quantity = quantity;
	    this.status = false;
	}
 
	@Override
	public void setDetails() {
	    this.status = true;
	}
 
	@Override
	public int getTotalCost() {
	    return costStrategy.calculateCost(quantity);
	}
 
	@Override
	public boolean getStatus() {
	    return status;
	}
 }
public class LaundryInputHandler {
    private Scanner scanner;

    public LaundryInputHandler() {
        scanner = new Scanner(System.in);
    }

    public int getWashType() {
        System.out.println("Enter type of wash (1/2/3): ");
        return scanner.nextInt();
    }

    public int getClothesQuantity() {
        System.out.println("Enter quantity of clothes: ");
        return scanner.nextInt();
    }
    
}
class PremiumWashStrategy implements CostStrategy {
	@Override
	public int calculateCost(int quantity) {
	    return quantity * 300; 
 }
}
class DeluxeWashStrategy implements CostStrategy {
	@Override
	public int calculateCost(int quantity) {
	    return quantity * 200; 
	}
 }
class BasicWashStrategy implements CostStrategy {
	@Override
	public int calculateCost(int quantity) {
	    return quantity * 100; 
	}
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant