Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java efficiency of moving methods to another class

Tags:

java

I'm currently currently working with a rather massive project with several classes of over 20, 000 lines. This is because it was someone's bright idea to mix in all the generated swing code for the UI with all of the functional code. I was wondering if it would incur any extra cost in terms of memory or run time, to move most of the non-UI related functions into a separate class. To provide an example, this is something along the lines of what I'm building.

public class Class1{
    private Class1Util c1u;
    List<String> infoItems;
    ...
    public void Class1(){
        c1u = new Class1Util(this);
    }
    public void btnAction(ActionListener al){
        ...
        c1u.loadInfoFromDatabase();
    }
}

public class Class1Util{
    private Class1 c;

    public void Class1Util(Class1 c){
        this.c = c;
    }
    public void loadInfoFromDatabase(){
        c.infoItems.add("blah");
    }
}

Eventually, I'd also like to move some of the fields like infoItems over as well, which would result in a reverse relationship, with Class1 accessing c1u.infoItems.

like image 501
Jainathan Leung Avatar asked Jun 04 '12 12:06

Jainathan Leung


2 Answers

no, separation of concerns is a good object oriented design practice. it will not cost you anything meaningful in terms of performance and will gain you many, many benefits in terms of maintenance, extensibility, etc.

like image 102
jtahlborn Avatar answered Nov 08 '22 20:11

jtahlborn


You may get a tiny performance hit for an extra level of dereferencing, but it would not be noticeable in a UI code, and you will get so much extra clarity in return that you wouldn't regret it.

Eventually you may want to externalize state keeping into a third class, and then use that state from both your hand-written and generated code, or use the Generation Gap Pattern to manage complexity introduced by the need to integrate with the generated code.

like image 38
Sergey Kalinichenko Avatar answered Nov 08 '22 18:11

Sergey Kalinichenko