I have an existing system that inserts books into a library database, and I want to generalize it to include other media.
The other media is similar, but not exactly the same, and all media are third party dependencies/models that I cannot change myself. (Otherwise I would have them all implement a single interface). Is it possible to do an Adapter, Converter, or Bridge pattern here?
public class libraryDAO {
private DatabaseConnection db;
public libraryDAO () {
this.db = DatabaseConnection.getNewConnection();
}
public insertIntoDB (Book book) {
this.db.insert(book.toMap());
}
}
public class Book {
public String title;
public String author;
public String coverArtId;
public Integer pages;
}
Now I have more media types this system needs to support (and imagine 10 others)
public class Film {
public String title;
public String director;
public Integer runtimeInMinutes;
}
public class Lyrics {
public String song;
public String lyricist;
}
So I naively created a huge class that can convert any of these media types:
public class Media {
public String title;
public String creator;
public Integer runtimeInMinutes;
public Integer pages;
public Media (Book book) {
this.title = book.title;
this.creator = book.author;
this.coverArtId = book.coverArtId;
this.runtimeInMinutes = 0;
this.pages = book.pages;
}
public Media (Film film) {
this.title = film.title;
this.creator = film.director;
this.coverArtId = 0;
this.runtimeInMinutes = film.runtimeInMinutes;
this.pages = 0;
}
public Media (Lyrics lyrics) {
this.title = lyrics.song;
this.creator = lyrics.lyricist;
this.coverArtId = 0;
this.runtimeInMinutes = 0;
this.pages = 1;
}
public Map<String, Object> toMap() {
//code that maps all the fields to a Map for DB insertion
}
}
And then update my libraryDAO like so:
public class libraryDAO {
private DatabaseConnection db;
public libraryDAO () {
this.db = DatabaseConnection.getNewConnection();
}
public insertIntoDB (Media media) {
this.db.insert(media.toMap());
}
}
But this doesn't scale, right? And it must be incorrect to have a giant class that just converts a wide-range of objects into a DB-acceptable state?
Is there a pattern that I'm missing? I can't use interfaces since I don't control the media types Book, Film, Lyrics, so I don't think I can use the Adapter, Converter, Bridge patterns.
Media
. Why don't you persist each library item in an individual database table, one which reflects the individual structure of each item type?Is there a pattern that I'm missing?
What's wrong with the never old fashioneddata mapper
? Basically, it's what you are doing right now inMedia
. So, it just takes you to create the right abstraction and move all that code into concrete implementations.