This project aims to address violations of the SOLID principles in a software codebase. The SOLID principles are fundamental concepts in object-oriented design, focusing on building robust, maintainable, and scalable software systems. In this project, we'll discuss violations of three out of the five SOLID principles and propose solutions to rectify them.
- The
Bicycle
class inherited astartEngine()
method from its superclassVehicle
. - Bicycles do not have engines, making the presence of
startEngine()
method inappropriate and violating the Liskov Substitution Principle.
- Refactored class hierarchy to separate vehicles with engines from those without engines.
- Introduced an abstract superclass
Vehicle
containing common attributes and behaviors. - Created a subclass
EnginePoweredVehicle
to represent vehicles with engines. - Removed the
startEngine()
method from theVehicle
superclass. - Implemented the
startEngine()
method only in subclasses representing vehicles with engines, such asCar
andMotorcycle
.
- Resolved violation of Liskov Substitution Principle by aligning class responsibilities and behaviors.
- Improved code clarity, encapsulation, and adherence to object-oriented design principles.
- Promoted better maintainability and scalability of the codebase.
- The revised design ensures that each class adheres to its defined responsibilities and maintains substitutability.
- Our codebase is now more robust, easier to maintain, and better aligned with SOLID design principles.
- This approach fosters a more organized and scalable architecture for future development efforts.
The Open-Closed Principle (OCP) is a fundamental concept in object-oriented design, emphasizing that software entities should be open for extension but closed for modification. This principle promotes code that is more maintainable, flexible, and scalable over time.
In our codebase, certain classes were found to be violating the OCP. Specifically, the PartTimeEmployee
and FullTimeEmployee
classes directly implemented the Employee
interface, which made it challenging to extend the system with new types of employees without modifying existing code.
To address the violation of the OCP, the following steps were taken:
-
Abstraction of Employee Types:
- Introduced a new abstraction layer to represent different types of employees.
- Created an abstract base class named
Employee
to define common properties and methods.
-
Implementation of Employee Subtypes:
- Refactored
PartTimeEmployee
andFullTimeEmployee
to extend theEmployee
base class. - Implemented specific behaviors for each employee type within their respective subclasses.
- Refactored
-
Refactoring Existing Code:
- Updated constructors, getters, and setters to reflect changes in class hierarchy and member variables.
-
Testing and Validation:
- Executed unit tests to ensure that refactored classes behave as expected and pass relevant test cases.
- Conducted integration tests to validate that changes do not introduce regressions.
After implementing these changes:
- The violation of the OCP was addressed, and the codebase now adheres more closely to object-oriented design principles.
- The
Employee
hierarchy provides a flexible structure for easy extension with minimal impact on existing code. - Refactored classes have been organized into the appropriate solution folder.
By proactively identifying and addressing violations of the OCP, we have improved the maintainability, flexibility, and scalability of our codebase. Adherence to established design principles ensures that our software remains adaptable to future changes and enhancements.
The Single Responsibility Principle (SRP) is a fundamental principle of object-oriented design that emphasizes that a class should have only one reason to change.
- The
Animal
class in the codebase is responsible for both storing attributes of an animal and simulating behaviors/actions. - This violates SRP as the class has multiple reasons to change, reducing modularity and maintainability.
- Modifications related to attributes or behaviors require changes in the same class, leading to coupling of responsibilities.
- Introduce separate classes:
Animal
andAnimalActions
. Animal
class stores attributes of an animal (species, age, diet).AnimalActions
class simulates behaviors/actions of an animal (eating, sleeping, making sounds).- Separation adheres to SRP, with each class having a single responsibility.
- Improves modularity, maintainability, and code quality.
- Changes related to data management or behavior simulation are isolated to their respective classes.
- Adhering to SRP improves code organization, modularity, and maintainability.
- Separating concerns into distinct classes enhances code reusability and reduces coupling.
- The proposed solution promotes better code design and overall code quality.
Author: Akshun Chauhan (Ak)
Date: 2024-03-21