Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Split a program into smaller objects

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Split a program into smaller objects

    Hello all,

    I'm coding a game called OuterSpace with some other people from JClass. I want to split the program to smaller files, the project isn't big at all. But I want it to be split from the beginning, so we don't have problems when it gets bigger, also we could learn from working that way too.

    I'm not really sure how to do it, or if there is a set order that's followed. The only thing I know is, the main class is the class with the GUI code, and were all the other classes are made into a functional program. Should there be a separate class for the ActionListener? Then make a new object in a thread, or just make a new object of the class that implements ActionListener?


    Any help and replies are greatly appreciated.
    -Mel


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,342
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Split a program into smaller objects

    There isn't really a strict rulebook to follow. A general guideline is that each method should do one thing, and each class should have one job. If you can't sum up a class or a method in a single sentence, you might consider splitting up its responsibilities. Extracting listeners is one place to start.

    Another general rule to follow is that your actual logic should be separate from your display code. This isn't always completely obvious, especially with games, but one way to look at it is "what if somebody else wanted to implement this game in a way that looks completely different, using my code?"
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Split a program into smaller objects

    First thanks for your quick reply Kevin.

    [QUOTE=KevinWorkman;64126]There isn't really a strict rulebook to follow. A general guideline is that each method should do one thing, and each class should have one job. If you can't sum up a class or a method in a single sentence, you might consider splitting up its responsibilities. Extracting listeners is one place to start.


    That's what I was thinking, but if you looked at the code, there isn't a lot of it. So I don't know if there is any use in making a new file for health, time, or points. The key listener code is the only thing that took a nice amount of lines to consider splitting.


    Quote Originally Posted by KevinWorkman View Post
    Another general rule to follow is that your actual logic should be separate from your display code. This isn't always completely obvious, especially with games, but one way to look at it is "what if somebody else wanted to implement this game in a way that looks completely different, using my code?"
    Do you mean AI when you say logic? Or everything to do with staying alive? If AI, there isn't any AI code atm, but I will keep your comment in mind since its one of the next things to code.

    Thanks again.
    -Mel

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,342
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Split a program into smaller objects

    Quote Originally Posted by Melawe View Post
    That's what I was thinking, but if you looked at the code, there isn't a lot of it. So I don't know if there is any use in making a new file for health, time, or points. The key listener code is the only thing that took a nice amount of lines to consider splitting.
    It doesn't sound like health, time, or points require their own files. But perhaps the display for those things could be in their own class that draws that part of the screen?


    Quote Originally Posted by Melawe View Post
    Do you mean AI when you say logic? Or everything to do with staying alive? If AI, there isn't any AI code atm, but I will keep your comment in mind since its one of the next things to code.
    I meant pretty much anything that would be considered your game engine versus its display. For example, movement, positions, etc. might be kept separate from whatever is displaying those things. If somebody wanted to port your game to use a different renderer, how hard would it be? What if they wanted to convert it to a command line game? Like I said, games oftentimes make the frontent/backend separation pretty fuzzy, but it's one way to think about things.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Split a program into smaller objects

    Ah, thanks.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Split a program into smaller objects

    If you have not read about them yet, you might study the basics of design patterns. They can be a bit overwhelming and confusing to begin with, but they are methods that have stood the test of time in helping with code re-usability, maintenance, and much more. Breaking things into smaller classes is one thing, doing it with a certain intent and having a set of rules to guide you towards that intent is another. Design patterns help you with the latter.

  7. #7
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Split a program into smaller objects

    Quote Originally Posted by copeg View Post
    If you have not read about them yet, you might study the basics of design patterns. They can be a bit overwhelming and confusing to begin with, but they are methods that have stood the test of time in helping with code re-usability, maintenance, and much more. Breaking things into smaller classes is one thing, doing it with a certain intent and having a set of rules to guide you towards that intent is another. Design patterns help you with the latter.
    Thanks Copeg. I actually was reading the wikipedia article on design patterns the other day. Though I didn't find much information there, do you know any site where they explain it in-depth?


    Thanks.
    -Mel

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Split a program into smaller objects

    Google 'java design patterns' and there are any number of sites that try and describe the patterns. All are important in one context or another, and all oftentimes have implementations in J2SE. For example:

    1) Observer - similar to the 'listener' types in Swing (ActionListener, KeyListener, etc...), it facilitates notifying observers when a change or event in one class occurs. My model (data, etc...) is often written completely separate from anything else (and access to the data is abstracted away to prevent concrete ties) and then plugged into other classes (such as a UI) using this pattern.

    2) MVC - sometimes listed as a J2EE concept, it is also a J2SE concept. Many Swing components are designed with this in mind. Take for instance a JTable - it has a TableModel (the Model), CellRenderers (the View), and event notifications such as ListSelectionListener, MouseListener, etc... (the Controller) - it even goes further to separate out other features, such as selection.

    I list the above two not just because they have implementations that serve as great examples for how and when to use, but also because they can serve a lot of importance in applications such as the one you are writing. They are not essential, but serve to make life much easier down the road
    Last edited by copeg; April 22nd, 2012 at 11:41 AM.

Similar Threads

  1. [SOLVED] Problem with split() method
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 21st, 2012, 05:15 PM
  2. Using char at to split up a interger
    By mooncowtime in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 26th, 2011, 08:55 AM
  3. Why does it get smaller when it's suppose to get bigger
    By velvetymold in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2011, 11:34 PM
  4. Help me split and then join a file
    By babbupandey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2010, 11:20 AM
  5. Bigger and Smaller
    By dylgod in forum Loops & Control Statements
    Replies: 4
    Last Post: July 28th, 2010, 09:18 AM