Ly Nguyen8,749 Points
How do you know where to put a file?
I saw that he created the com.teamtreehouse.model package for Song and Songbook and put those files inside the model directory. However, Karaoke.java was placed at the same level as com and KaraokeMachine was placed at the same level as the teamtreehouse "package," he said. Why is KaraokeMachine.java not in the model directory with Song and SongBook or at the same level as Karaoke.java? How do you know where to put files?
Alexander NikiforovJava Web Development Techdegree Graduate 22,128 Points
Packages in Java is used to group different classes with the same functions and logic. Roughly you can think about them as folders where you put your classes files.
In Java Application design it often comes to Model-View-Controller design pattern.
Here is a nice vide from 'Spring Basics' course
You can watch it, it is very introductory.
And it is naturally to group classes by this logic, i.e. put the classes to
I would say the that best to understand how and why we group classes, is to go through 'Java Web Development' track. In its courses you will see a lot of packaging :
- Dao (Database)
And others ...
Now coming back to why we put
model package, and why
SongBook are typical model classes, because they represent the base entities, that can later for example be saved in database, and usually have only getters and setters. Model classes they are like bricks, at the very bottom of Java Program.
Let me try to explain on Web Application
- Song Model: just members, and getters/setters.
- Dao (Database). Here you will have classes that will
- Service. Here you can for example do security checks, which user can
saveSongs using Database layer
- Controller. Here you use Service classes to get songs, and create methods that will process generation of Web Pages
- View. HTML files where variables from Controller will create HTML pages
It may sound like an overkill, but again, it is one of those, comes with experience things. If you stick to Java Web Dev Track, by the end of it you will definitely see how and why people name packages.
One last thing. About why
Karaoke is just in
com.teamtreehouse and not in
view package ...
That is because
Karaoke class is so-called "Application Runner" class.
Because it has this
public static void main(String args)
People usually put them outside, in "root" package. Because we use them to run the program. By Running this class, you actually run your program.
And in super 100 classes Web apps, there is always one "Application Runner" classes that is just like
Karaoke comes in the "root" package.
Ly Nguyen8,749 Points
Thank you, Alexander! I am in the Java track and definitely looking forward to learning more about the MVC design pattern. I understand now why Song and SongBook are a part of the model (M) and so are in the same package and the Karaoke class is at the root to run the program and contains the main method.
I am still a little confused about the KaraokeMachine class and file. Is it the C in MVC? What part does it play? I am still uncertain as to why it was placed at the same level as the model folder and not at the root.