Exercise: Transforming Shape Area Calculation

Let’s understand how to refactor code calculating the area of rectangles to adhere to SOLID principles and good OOD.

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 and the Open/Closed Principle.

  1. Refactor the code to separate concerns so each class has a single responsibility.

  2. Ensure that the code can be easily extended to handle other shapes (e.g., circles) without modifying existing code.

  3. Make sure the code is modular and follows good object-oriented design principles.

Try it yourself

Here is the coding example of calculating the area of a shape(Rectangle) before refactoring.

Get hands-on with 1200+ tech skills courses.