3 Replies - 5272 Views - Last Post: 08 August 2012 - 07:14 AM

#1 ricky116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-August 12

Does this Java Design Make OO Sense?

Posted 07 August 2012 - 01:52 PM

Hi everyone, I am fairly new to Java still and want to work on my first 'real' project to try and get to grips turning concepts (mainly from Head First Java) into real-life code.

I am having major trouble 'fitting together' various classes and methods together such that my application conforms with OO principles. Deciding what should be a class, what shouldn't, what the responsibilities of each class should be etc... it's a bit of a headache at the moment so any help is very welcome.

My first go at diving straight into code (despite well intentions) quickly turned into having a 'god' object that held everything, and I don't think I had it right.

I want to make a relatively simple movie-information viewing application with swing that lets you log-in as a user from a combobox list (from a .txt file), and then displays a main window that includes a JList of movies (from a .txt file) and genre checkboxes to filter the JList.

---

The design:

Manager class
main () calls new Manager.start()
start () makes a new LoginFrame object

LoginFrame class extends JFrame
constructor sets up the GUI, creates new Helper object that fills the combobox from the file, and sets the 'Login' button ActionListener.
LoginButtonListener calls this.login()
login() makes a new MainWindow object, passing the selected user, and disposes itelf.

MainFrame class extends JFrame
constructor sets up GUI, the movie JList is filled from a new Helper object.getMovies method, and sets up the actionListener for the checkboxes.
CheckBoxListener calls this.updateJList()
updateJList updates the JList by looking at which checkboxes are selected

There are also User and Movie classes, which are returned by Helper as ArrayLists by Helper's methods.

---

Typing that out actually helped a lot, but I am still unsure of whether or not I am on the right track. When the MainFrame object is made, the LoginFrame object is no longer needed but is still on the stack because the MainFrame has been created from its login() method? If I were to offer a change-user button from MainFrame, I would then make a new LoginFrame, which would then call a new login() method... and I would have 2 'live' MainFrames... right?

Any advice is greatly appreciated!

Thanks,

Richard

I should mention that I want to be able to login as a unique user, because a list of movieIds 'seen' is included with a user object so that besides genre checkboxes I want to be able to choose whether to display all movies, seen movies, or unseen movies. I kept the OP as simple as I could to focus on structure.

Is This A Good Question/Topic? 0
  • +

Replies To: Does this Java Design Make OO Sense?

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Does this Java Design Make OO Sense?

Posted 07 August 2012 - 02:34 PM

That sounds like a logical set of objects to me. You might end up needing more and that's OK. With regards your concern about multiple login boxes remaining in memory, the rules are quite simple. If you call the dispose() method after you hide a login frame, all it's graphical resources will be freed. If you also make sure that all variables referencing it are set to null, it will be marked for garbage collection.

God Objects

We all end up making classes with too many responsibilities sometimes. You don't have to start from scratch to fix them. In essence, what you do is to restructure the existing code in such a way as it can be split between other classes.

If you have too many member variables, look for groups that go logically together. Username and password might belong in a login credentials class. Start and end might go in a range class. Another good way to see what might belong together is to see what data types keep turning up together as arguments to method calls. Group those fields in a class and not only does your god object get simpler, so do your method calls!

Another tip is to move the method to where the data is. If you gave a method called getLength(int start, int end) then it is a great candidate to make a method in the Range class I was talking about above so you can call myRange.getLength().
Was This Post Helpful? 1
  • +
  • -

#3 ricky116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-August 12

Re: Does this Java Design Make OO Sense?

Posted 07 August 2012 - 03:15 PM

View Postcfoley, on 07 August 2012 - 02:34 PM, said:

That sounds like a logical set of objects to me. You might end up needing more and that's OK. With regards your concern about multiple login boxes remaining in memory, the rules are quite simple. If you call the dispose() method after you hide a login frame, all it's graphical resources will be freed. If you also make sure that all variables referencing it are set to null, it will be marked for garbage collection.

God Objects

We all end up making classes with too many responsibilities sometimes. You don't have to start from scratch to fix them. In essence, what you do is to restructure the existing code in such a way as it can be split between other classes.

If you have too many member variables, look for groups that go logically together. Username and password might belong in a login credentials class. Start and end might go in a range class. Another good way to see what might belong together is to see what data types keep turning up together as arguments to method calls. Group those fields in a class and not only does your god object get simpler, so do your method calls!

Another tip is to move the method to where the data is. If you gave a method called getLength(int start, int end) then it is a great candidate to make a method in the Range class I was talking about above so you can call myRange.getLength().


Thanks cfoley, you've explained moving groups off into classes in a way that makes it much simpler than I have been making it out to be in my head. I will also read up on memory management and the GC as I think I may have forgotten a few of the basics.

Off now to break my big class down into the above, thanks!

I've also bookmarked these forums, hopefully a bit of knowledge here and that trick there will lead to strong understanding.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: Does this Java Design Make OO Sense?

Posted 08 August 2012 - 07:14 AM

There are a couple of ways to deal with the messy "Login form launches Main Form and left hanging" problem.

One way is to have the main form lauch the Login form. Then when the login is completed, the main form can do clean up. However, programming wise, this can get kind of messy.

The second way, interestingly, is a lot like what you have in mind. You have a main class that fires a Login. Then the login completes and the main class decides what to do with the results.

You'll want that login modal, in either case. Extend from JDialog, not JFrame.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1