lundi 20 décembre 2021

Refactoring a class to comply with single responsibility principle

Consider a shape with a color, a width, and a height. Each shape should compute its own perimeter and have a method able to draw itself. A Shape allows sorting a list of shapes. To allow sorting the shapes by comparing their surfaces, each shape should be able to compare itself to another shape using the compare method of the Comparable interface. Each Shape should support the instantiation (i.e., creation) of objects belonging to the Shape class or to the classes that inherit from the Shape class (e.g., the Circle class).
How to refactor this to comply with the Single responsibility principle?

My thoughts:
The only problematic thing I see with this design is that the Shape should support the instantiation of objects. So this behavior can be removed from the shape class, and probably a separate class called factory could be made to perform this operation.
Not sure if there is any other refactoring one could do

Aucun commentaire:

Enregistrer un commentaire