Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 10/28/2015 in all areas

  1. In this stance your Cat and Dog hierarchies are actually different functionally - one Meows and the other barks. Your find Animal and logic after that presumes to work on any Animal - but Animal doesn't have a bark or meow. This is an LSP violation for the caller that expects an animal to do something specific. Your use case of the API (your example) is that the animal needs to make a sound - bark or meow is irrelevant. In this instance I think it is cleaner to: - Have a Make Sound method in Animal that Cat and Dog base classes over-ride to Meow or Bark. - if it is important to be able to make any Cat meow specifically then you are better off adding a "Make Meow" and "Make bark" to your Cat and dog base classes that is only available to them and their children. This way your clarify to your callers that any animal can make a sound but only cats meow and only dogs bark. The best approach here is probably to favor composition and move sound making into it's own hierarchy of classes that are composed into your animals but that's a whole different story.
    1 point
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.