I was reading this page and came across this sentence in the accepted answer:
I don't like
A
directly knowing aboutB
. But that's a DIP thing not a POJO thing.
What if you can't abstract out B
so that A
isn't aware of it?
Suppose if I have the following Book
class:
public final class Book {
private final Author author;
// constructor and methods left out :D.
}
public final Author {
private final String firstName;
private final String lastName;
// constructor and methods left out :D.
}
I'm aware of the fact that a book might have a list of authors, so it would be better if it was List<Author> authors
, but I want to focus on a specific part of the above code sample.
Some might point out that the Book
class knows about the Author
object.
I don't see why Author
would be an interface
or abstract
class.
Question:
Would you abstract out Author
? If so, how?
List
collection calledgenres
, to indicate that one author might write multiple types of books. – Oct 14 '18 at 21:18mainGenre
property, you need one function forGetPrefix
that has a big switch-case statement depending on theirmainGenre
, 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 ownGetPrefix
, it's easy to add a new genre or update an existing one with minimal chance of impact. – IllusiveBrian Oct 14 '18 at 22:23findLastElement(List list)
. HereList
is an interface that can either be substituted by anArrayList
or aLinkedList
, making this method more general than if it wasfindLastElement(ArrayList list)
instead. – Harsh Verma Oct 15 '18 at 01:53