Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a Java project that is getting to be relatively large (a few thousand lines in all, I'd say). It's definitely a lot larger than what I've dealt with before, being that I am still a relatively new programmer.

I have a class structure with my swing components and other classes I have made that, more and more, is becoming confusing. Even as a beginner, I can tell this wouldn't be considered "good programming practice". It's somewhat difficult to explain, and I cannot simply copy/paste code, as this is really dealing with the whole program. But I will do my best to explain...

As an example, I have a "Minion" class, and Minion objects will sometimes do something that needs to update a "levelGUI", an extended JPanel. How do they access this levelGUI? I will tell them something like "Game.getLevelGUI().someMethod()" and that just looks sloppy. Am I wrong?

The problem is that I have gotten ahead of myself in actual programming, relative to my understand of how the structure of said program works. I think what I will end up with, if I continue down this path, is a program that works as I intended, but with poor readability and great difficulty in altering down the road.

If this seems a difficult question to answer, links to articles which could explain how a large hierarchy of objects effectively "interact" or something to that effect... would be VERY helpful (I'm not being lazy, I have looked and looked, trying to find something).

share|improve this question
1  
The other way around is better: Have your LevelGUI class inspect your Minion object and then draw itself accordingly. So your Minion never actually have to deal with its representation. Or you can have a third class mediate between the gui and the minion as a controller. –  Martin Wickman Apr 25 '11 at 15:48
add comment

migrated from stackoverflow.com Apr 25 '11 at 11:07

This question came from our site for professional and enthusiast programmers.

3 Answers

Few things you can consider step by step with refactoring your codes. As based on short idea about your project, Here short guidelines make it little better rather than rewrite you codes a lot.

  1. Try to separate your GUI codes out and then within that you could think about further improvement such as model view controller model.

  2. Try to move common class used for utilities to utility module and import that jar to required module.

  3. If any database or core coding those could moved as core module.

  4. Try make use the java package naming standard way to group the classes.

  5. Add more comments and improve readability with coding standards tools,

And if possible analysis your project codes with Sonar : http://www.sonarsource.org/ it guide you move to improve.

share|improve this answer
add comment

My all-time favorites resources about organizing guis are by Martin Fowler

http://martinfowler.com/eaaDev/uiArchs.html

http://martinfowler.com/eaaDev/OrganizingPresentations.html

and the patterns described there

share|improve this answer
add comment

I have had a similar experience when trying to do a Swing project after a long long time. Having developed web applications I can see some patterns right away to solve these kinda issues, but in Swing everything was looking blur.

Few things I like to share from my experience.

  • I followed MVC. Think of moving navigation control and stuff to controllers.
  • I followed Mediator, in order to decouple different controllers. Precisely, they are there providing a way of communication between controllers.
  • I have also spotted a use of Observer pattern in my project. You should read that too, and then see if something can be solved using that.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.