Timeline for Is it bad practice when an Object has to be aware of another Object
Current License: CC BY-SA 4.0
6 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 15, 2018 at 1:53 | comment | added | Harsh Verma |
@BasementJoe I see your concern. Let me elaborate on the scenario I was trying to highlight. Assume you have a method findLastElement(List list) . Here List is an interface that can either be substituted by an ArrayList or a LinkedList , making this method more general than if it was findLastElement(ArrayList list) instead.
|
|
Oct 14, 2018 at 22:23 | comment | added | IllusiveBrian |
@BasementJoe This is getting contrived because this example is conflating data and functionality, but imagine that the company's ID for an author has a unique prefix depending on their main genre. If you have a mainGenre property, you need one function for GetPrefix that has a big switch-case statement depending on their mainGenre , that has to be updated anytime a new genre comes out or any prefix is changed. On the other hand, with different subclasses all implementing their own GetPrefix , it's easy to add a new genre or update an existing one with minimal chance of impact.
|
|
Oct 14, 2018 at 21:18 | comment | added | user315575 |
The problem is, I don't see why different subclasses of Author need to exist. The types you provide in your first point could just as easily be stored in a List collection called genres , to indicate that one author might write multiple types of books.
|
|
Oct 14, 2018 at 20:00 | comment | added | Martin Spamer | This answer provides a good example of why in this case, the more general reasoning it that this approach make the software more flexible with regard to enhancement and is closely related to the Open Closed principle en.wikipedia.org/wiki/Open%E2%80%93closed_principle | |
Oct 14, 2018 at 19:40 | review | First posts | |||
Oct 17, 2018 at 7:32 | |||||
Oct 14, 2018 at 19:35 | history | answered | Harsh Verma | CC BY-SA 4.0 |