The Artima Developer Community
Sponsored Link

Java Answers Forum
Java program structure

2 replies on 1 page. Most recent reply: Mar 10, 2005 3:06 PM by cBin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Java program structure Posted: Mar 8, 2005 3:30 PM
Reply to this message Reply
Advertisement
Hi, new here and to Java.
What is the best way to structure programs? in the books all the programs are only a page or so long and only one or two classes, but most useful stuff is longer than this. How do you deal with this, I imagine that it would be a mess to have pages and pages of code all in one file, but I may be wrong? Do you make new packages for each major class, and then reference the packages into the file that has your main function? If so, how do you do this? Do you need an import statement like you do for a library? Does someone know of a program that I can look at that is pretty simple, but is set up like a real size program actually is?


Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Java program structure Posted: Mar 9, 2005 10:15 AM
Reply to this message Reply
First, a disclaimer. I'm somewhat self-taught via lots of time reading tutorials and forums (this one, in particular, is the best I've found) so a lot of what I'm going to put forward is only my opinion. In my defense, though, I will say that I do have a lot of background in non-Java programming and object modeling using UML. So, a lot of what I'm going to throw out here, I believe, is just generally accepted good practice. I will, however, defer to the rest of the forum--they generally are more experienced than I.

My industry is vacation rental property so the business objects I deal with are things like Property, PropertyOwner, Reservation, etc.

My classes are directly derived from my business objects, so I have a java class called Property, for instance, which has all the relevant Property attributes and methods (getPropertyName(), deleteProperty(), etc.) along with any methods which use SQL to reference the Property table in my database. I use Swing components to provide end users with access to these objects. So if an end user needs to change the Property address, they pull up a Property JFrame, the JFrame instantiates a Property object, which in turn gets its data from the database and makes it available to the JFrame for display. The user does their thing, hits the 'Save' button and the JFrame scrapes the data from it's text fields, calls setter functions from the previously-instantiated Property object, and then asks the Property object to save itself to the database. The Property object has it's own methods to edit the data and determine whether to call an insert or an update method.

While my classes are derived from my business objects, my packages are more broadly based. I have one for "Reservation Management", which includes Property, Reservation, etc.; then another one for "Financials", which has classes like ReservationPayment, Expense, OwnerPayment, etc. Also, all my packages have sub-packages called UI, which has the various Swing components such as the one I described above. I also have my own "util" package for things like date formatters, string formatters, JDBC stuff, etc.--tools I had to write that are not specific to a business area.

I don't know if this is how others do it (in fact, I'm really interested in seeing what some of the other responses are) but it works for me.

I would strongly recommend that you spend a little time learning object modeling and UML. It makes it much easier (for me, anyway) to first define the business requirements in terms of business objects, then use Java to implement those objects.

cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Re: Java program structure Posted: Mar 10, 2005 3:06 PM
Reply to this message Reply
Thanks Ken, That actually helps a huge amount! I am trying to think about this stuff a little before I start making a mess.
Anyone else want to weigh in?

Flat View: This topic has 2 replies on 1 page
Topic: Java Script questions- URGENT Previous Topic   Next Topic Topic: sound mixing in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use