• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with organizing code  RSS feed

 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again all;

On my project I am creating the landing window were - once a technician logs into the application this page shows.  I am using "windowsbuilder" in eclipse.   Eclipse created the main method as seen below:




And also created the class "landing()" as:



One thing my application is going to need to do is read a database and create different objects based on the data being fetched.  For instance the program is going to monitor the weekly commission sheet and display it via a table.  Where in this deign would best practices suggest I place the working code to populate the tables and such?  I was thinking I should write a method called initialize, where all the code will be to get the required data .  Then at some point call this method.  I have a couple of ideas (but I haven't written code yet).

One idea is to put the different variables right under the "public class landing extends JFrame{" --> then call the method to get the data and populate the variables.  Then in "public landing(){"  I can use the variables to populate the different elements.  For instance there will be an object of "weekending" for each week the user has time saved to it.  When the program loads I want a combo box to be loaded with all the weekending objects.  Then when the user selects a specific weekending a table will populate with all the repair orders and the times they claimed.

I apologize now if this post is a little confusing, I took sleeping pill and wanted to get this question posted before I went to bed.

Thank You all for the help!  




 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find the edit button.  I did not create the class "public landing()" windowsbuilder did.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An important concept in design is separation of concerns. There is also the Single Responsibility Principle. Try to keep database concerns separate from business domain concerns and those separate from display concerns. If you mix together different concerns, you will likely break SRP, making your classes more coupled and having to change for multiple reasons.
 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu I remotely remember that from school.  Although I was thinking this question more literal.  For example the no I use the main routine to call code that is used to fill the form, leaving the landing class only for design?  As I was thinking but didn't have time to test yet.  Based on the SPR you commented about -- The landing() should be only used for the design (look) features of the form.  Then I can either put calls to other function to occur before or after the landing() is created in the main routine.   
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to think of an application that's written in Java as a community of objects working together to accomplish something good. There has to be harmony and coordination within that community of objects for that to happen. As one erstwhile character in "The Walking Dead" used to say, "Everyone has a job to do." So, besides the SRP, you also have to think about how to coordinate the different responsibilities and get one object to work with other objects to get a larger task done.

You might start with the Landing (LandingPage is probably a more expressive name) and tell it that it needs to work with some objects that represent what a technician sees when he/she logs in and views the landing page. That is, you tell the LandingPage object, "Hey, ask these objects over here for information you need to display. Your job is to make sure that information is display in a way that makes the technician's job easier."  If you decide to orchestrate an interaction like this, you're probably going to need somebody (another class that is, or maybe a framework like Spring to do dependency injection) to help hook the LandingPage up with the appropriate domain objects that will provide the information the LandingPage is tasked to display.

Then you go to those domain objects and make them work with a Database object, whose job is to ferry information to/from a data repository and populate different domain objects with that information or take information from domain objects and store it back to the repository.

These are just examples of different interactions you'd have to design and codify in your program.
 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar, I was contemplating your last post pretty well all day.  referring to my CS1102 class where we discussed the idea that each class should have one job and that each actionable element should have three separate classes.  One for the design, one for the "business logic" and one as a manager.  I realized I was making a huge faux-pa in Java.  I was actually trying to build the application like I used to back with VB6.0.  I was considering the landing page as the "main" program element.  That is not what I should be doing!  Or at least I think I finally understand.  I need to write a main control class that loads when the program is loaded.  This class tells the "landing()" page to open.  It also would call another class for the work with retrieving the data.  I attached a quick rough idea of my thinking.  Noone really ever sees the main class running, you only see the windows classes it commands to open. 

This creates a question.  Is there a way to create a project file that holds all the global variables most of the pages might need?  Or should this be part of the programs controller class? 

Is this the idea you were getting at?

Thanks        
rough-organization.JPG
[Thumbnail for rough-organization.JPG]
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that went better than I expected. Good job on getting a good understanding of the concepts and applying it to your problem's context. That diagram is a pretty accurate depiction of what I was going for.

As far as the question about global variables goes, my gut reaction as an OO programmer/designer is "Eww, global variables." 

Try to question the need to make any variable globally accessible. Does it really have to be accessible by any class/object?  Is there a particular class or object to which the variable is more closely associated conceptually/logically? Would it be more appropriate for that variable to be attached to that class or to a particular instance of that class instead? The answers to those questions are usually no, yes, and yes, respectively, although sometimes it may not be immediately apparent to you which particular class or object it is.  With some trial and error, you can almost always find a class that can host the variable in a way that makes sense from an OO perspective.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!