I need to develop a list of libraries that will hold multiple library types. For example, the library can be a library of videos, or a library of photos. I want to follow the MVC design pattern using JavaFX if this is of relevance. Please see the UML diagram below in reference to my question:
Figure 1

So I figured I'd use a generic type for the list in my Library Model like this:
public class LibraryModel<T> {
private List<T> aList;
}
However, this won't work because when I instantiate the list of libraries, I need to specify a type. So in this way, I can only specify either a Photo or a Video library as a list, not both.
I tried to introduce a generic type called "Library" that will be listed in the list (as a superclass) to be extended by the PhotoController/VideoController:
Figure 2

public class LibraryModel {
private List<Library> aList;
}
In figure 2, I could create a list of generic libraries. But how will this affect my ability to perform specific photo/video tasks? Let's assume I have the following methods:
Superclass methods:
displayAll()
add()
remove()
Photo library methods:
displaySlideShow()
Video library methods:
playVideo()
By storing the libraries as the superclass, how will I be able to call the specific methods of the other library types? Is this even legal?
Please go easy on me, I'm still learning Java, thanks in advance.
Use abstract class
the Library.
abstract class Library {
}
then create child classes.
the photo library.
class PhotoLibrary extends Library {
displaySlideShow(){
}
}
the video library.
class VideoLibrary extends Library {
playVideo() {
}
}
in main class or another you can use them.
private List<Library> aList;
aList.add(new PhotoLibrary());
VideoLibrary l2 = new VideoLibrary();
aList.add(l2);
then you can call available methods using,
Library l = aList(i);
if(l instanceof PhotoLibrary) {
PhotoLibrary pl = (PhotoLibrary)l;
pl.displaySlideShow();
}
else if(l instanceof VideoLibrary) {
VideoLibrary vl = (VideoLibrary)l;
vl.playVideo();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With