Results 1 to 5 of 5
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default Problem to organize my code in classes

    Hi everyone,

    I'm just confused about how to organize my code.

    Basically I have a GUI and other methods to load/write data from/into files.

    For now, I just have one BIG class with everything inside but of course I know it's not that good and I would like to dispatch my code into classes.

    I thought about creating one general class and put the methods that load/write files in that class and create another class just for the GUI. But actually, there are variables that I need in the load/write methods AND in the GUI too.

    So, should I create this extra class for the GUI and make it "extends" the general class (where the main is, and the load/write files methods are) ? I'm not sure about this "extends", I mean if it's the right choice for my situation.

    Can someone help me please ?

    Thank you for your help.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ze snow View Post
    I thought about creating one general class and put the methods that load/write files in that class and create another class just for the GUI. But actually, there are variables that I need in the load/write methods AND in the GUI too.
    So create a third class that contains those variables. Typically that class is the 'model' of it all; the 'view' part is composed of your gui class(es). The control of it all should be implemented in the 'control' part of your application. In your case the control part contains the reading and writing of those variables. This is the classic MVC pattern (Model, View, Controller). Try to stick to this separation, it'll pay back in the end.

    kind regards,

    Jos

  3. #3
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Ok thanks for that tip JosAH, I'm rearranging the code according to this now.

    I also have another class actually which I wrote to create my own type. Can I leave this class as a "fourth" class or should it be included in the 'model' class ?

    According to what you said, does it mean that I can basically run with 3 classes all the time ?
    Do my methods that load & write from/into files have to be part of the 'model' class as well ?

    Thank you

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ze snow View Post
    I also have another class actually which I wrote to create my own type. Can I leave this class as a "fourth" class or should it be included in the 'model' class ?

    According to what you said, does it mean that I can basically run with 3 classes all the time ?
    Do my methods that load & write from/into files have to be part of the 'model' class as well ?
    Divide your application into as many classes as you need; a 'view' or 'controller' or 'model' doesn't necessarily consist of a single class. If you'd do it that way you'll end up with 'god classes' or 'swiss army knives', i.e. those classes would do too much, something other classes could do better. Try to find information on 'CRC' (Classes ResponsibilityCollaboration), it'll explain well how to divide your application into classes.

    You gui already consists of more classes: frames, dialogs, tables, text fields etc. Those are not yours, your application only uses them but nevertheless ...

    kind regards,

    Jos

  5. #5
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. Source code of Predefined classes and methods
    By lobodelbosque in forum New To Java
    Replies: 8
    Last Post: 08-10-2011, 02:11 PM
  2. Passing values between classes problem.
    By alin_ms in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 06:49 PM
  3. How to divide code in classes?
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 05:36 PM
  4. [SOLVED] Using Code Throughout 5 Classes-Stuck
    By Bascotie in forum New To Java
    Replies: 46
    Last Post: 06-06-2008, 05:51 AM
  5. Organize class source code
    By Alejandro Valdez in forum Eclipse
    Replies: 0
    Last Post: 05-16-2008, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •