Java Java Data Structures Efficiency! Design the UI

Ly Nguyen
Ly Nguyen
8,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?

3 Answers

Alexander Nikiforov
Alexander Nikiforov
Java 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.

https://www.tutorialspoint.com/design_pattern/mvc_pattern.htm

Here is a nice vide from 'Spring Basics' course

https://teamtreehouse.com/library/spring-basics/using-the-mvc-architecture/what-is-mvc

You can watch it, it is very introductory.

And it is naturally to group classes by this logic, i.e. put the classes to model, controller, view packages.

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 :

  • Model
  • Dao (Database)
  • Service
  • Controller

And others ...

Now coming back to why we put Song and SongBook to model package, and why Karaoke to com.teamtreehouse:

Song and 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 save, update, list and delete songs.
  • Service. Here you can for example do security checks, which user can save Songs 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 controller or view package ...

That is because Karaoke class is so-called "Application Runner" class.

Why ?

Because it has this main method

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 Nguyen
Ly Nguyen
8,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.

Alexander Nikiforov
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,128 Points

You are right.

KaraokeMachine is the Controller class.

It is better to be put in controller package.

Craig didn't do it because, well we have a very small application. It does not actually matter where you put classes, for now.

But yes, In my opinion it is better to put KaraokeMachine class in the com.teamtreehose.controller package and Karaoke as "Application Runner" in the root package com.teamtreehouse.

One more thing I wanted to add, that there will be no view classes, because our View part, I think (I'm not super expert) , is actual console where you write things...If you have Web Application, view is actually HTML file, if you have IOS, view is storyboard, and its xml file.

And also wanted to add, that packaging is important for separating logic, it is needed for convenience. And for example in business, people use so-called 'package-by-feature' style:

http://www.javapractices.com/topic/TopicAction.do?Id=205

If you ever get to 'Spring Rest API' there Craig will give you example of such style.

You can watch the video below, some part of it just about this

https://teamtreehouse.com/library/entities-and-repositories-review

And also you can note yourself, that no one use this model, controller packaging naming when you re-use classes, for example take a look at classes you've imported, none of them has model, or controller.

So don't think too much where to put classes for now. You have only 4 of them, so it is not a big deal where are they.

Once your application becomes bigger you can start thinking about it, for example in Web Applications or GUI applications, there structure is very important, and in those courses packaging will come naturally and with examples.

One thing you should notice from the way classes organized is the following:

Note the difference between KaraokeMachine and SongBook. KaraokeMachine is merely controlling how SongBook is changed: prompting, adding, getting.

And method byArtist is inSongBook class, because it contains logic how SongBook is organized.

Note that we also made addSong method in SongBook, and we don't write in KaraokeMachine the following

// imagine that we have getter for mSongs called getSongs
mSongBook.getSongs().add(song); // this is violation of model-controller relationship

We however do write

// correct model-controller relationship, because we hide what type 
// actually mSongs are
mSongBook.addSong(song) 

It just comes with experience, and later on you start seeing it clearer.

Ly Nguyen
Ly Nguyen
8,749 Points

Ahh. This makes a lot more sense now. Thanks a lot!