开发者

Java Class Organization

I am making a multiplication time attack program, which currently is in a very early stage. I have it split into 4 classes. MainApp, which initilizes the other classes and sets up the JFrames. Settings, which disp开发者_开发知识库lays a screen where people choose which numbers to test. MainWindow, which will be where the numbers will be displayed and where the user answers. My last class is Calculate, where the questions and answers are made. My problem is that Calculate needs which check boxes are checked from Settings. Calculate needs to know when the user is done changing Settings. MainWindow needs which numbers to display from Calculate. Is this normal to have such a large circle of classes, or is my class organization wrong? And since Java passes by value, wouln't giving each class a copy of another class just for a copy of an array of 12 booleans use double the memory? Eg. If Calculate requires 10 MB of data, wouldn't giving MainWindow a copy of it in the constructor (shown below) cause 10 MB more of RAM to be taken, just to retrieve 12 bits of data?

public MainWindow(Calculate calc) {

    super("Main Window");

    // Display Window
    pnlMaster = new JPanel(new BorderLayout());
    pnlLabels = CreateLabelArea();
    pnlButtons = CreateButtonArea();

    pnlMaster.add(pnlLabels, BorderLayout.CENTER);
    pnlMaster.add(pnlButtons, BorderLayout.SOUTH);
    this.getContentPane().add(pnlMaster);       

    // Handler 
    cHandler handler = new cHandler();
    btnNext.addActionListener(handler);
    btnQuit.addActionListener(handler);
    txtAnswer.addActionListener(handler);

    // Get Calculate class
    this.calc = calc;
}     


No, objects are actually references to the object. The references are passed by value to the method (e.g. it's a copy of the reference to the object, not a copy of the object itself).

What this means in practise is that there's only one copy of the data in the case above, so no need to worry about extra memory.

In terms on whether your class design is right/wrong, the most important thing is separating out the logic from the presentation. It sounds like you are on the right tracks with this, but it wouldn't hurt to read up on MVC (model view controller) because that's a common pattern for this kind of problem.


Classes are passed by pointer in Java.

It actually passes a pointer to the object, not a copy of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜