Exercise: Transforming Shape Area Calculation
Practice how to refactor code by calculating the area of rectangles while adhering to SOLID principles and good OOD.
We'll cover the following
Task and requirements
The provided code calculates the total area of rectangles but does not adhere to SOLID principles. Your task is to refactor the code to make it conform to the SOLID principles, especially the Single Responsibility Principle (SRP) and the Open/Closed Principle (OCP).
Refactor the code to separate concerns so each class has a single responsibility.
Ensure that the code can be easily extended to handle other shapes (e.g., circles) without modifying existing code.
Make sure the code is modular and follows good object-oriented design principles.
Try it yourself
Here’s a code example of calculating the area of a shape (rectangle) before refactoring.
Get hands-on with 1400+ tech skills courses.