My idea was that maybe it violates at least I? Because what if a pet does not have fur (but the interface is general purpose). Which SOLID design principles does it violate? I am not sure if it violates D, since there are abstractions. But are they good enough? I don’t think it violates S or O. Maybe it violates L because you can’t replace the subtypes? In this case what is a subtype?
I would think it would make more sense instead of having a class pet printer to just have a print method on each pet. That way you wouldn’t have to modify it each time something in a pet changes. I am still new to this though. Btw I didn’t make this code, it came from an online assessment, which I failed several months ago, but was just wondering about in the back of my head (They don’t tell you which ones you got wrong).
interface i_pet string get_name() string get_species() color get_fur_color() end interface interface i_cat implements i_pet end interface interface i_fish implements i_pet bool get_is_fresh_water() end interface class cat implements i_cat string name string species color fur_color cat(string name, string species, color fur_color): this.name = name this.species = species this.fur_color = fur_color string get_species(): return species string get_name(): return name color get_fur_color(): return fur_color end class class fish implements i_fish string name string species bool is_fresh_water fish(string name, string species, bool is_fresh_water): this.name = name this.species = species this.is_fresh_water = is_fresh_water string get_species(): return species string get_name(): return name color get_fur_color(): throw exception bool get_is_fresh_water(): return is_fresh_water end class
class pet_printer print_pet_name(i_pet pet): print pet.get_name() print_fur_color(i_pet pet): print pet.get_fur_color() print_pet_species(i_pet pet) print pet.get_species() print_pet_type(i_pet pet): switch type of pet: case i_fish: print "Fish"; case i_cat: print "Cat"; default: print "Unknown" end switch end class