vendredi 25 novembre 2016

Class that does things to another class

I have a class that is essentially a wrapper for a large data object on a database. Looks like this:

public class ServerWrapper {
    private DataObject object;

    public ServerWrapper(DataObject object) {
        this.object = object;
    }

    public void doAThing1() {
        getSomeStuff();
        // do stuff that modifies this object
    }

    public void doAThing2() {
       getSomeStuff();
       // do other stuff that modifies this object
    }

    private List<> getSomeStuff();
}

This is the problem: there are many, many "doAThing" methods. And some of them are quite large. Also, a lot of them use other private methods also in ServerWrapper. Ideally, I'd like to break off these public methods into their own classes, like ThingDoer1, ThingDoer2, but I don't know the best way to do this.

Something like this:

public class ThingDoer1() {
    public void doAThing1(ServerWrapper wrapper) {
        wrapper.getSomeStuff();
        // do the thing to wrapper
    }

seems very smelly; it's tightly coupled to ServerWrapper (ServerWrapper calls it and it calls ServerWrapper), plus it needs to either do stuff with the object it's given (which is bad), or make a copy, do the stuff, then return the copy.

Really, I think what I'm looking for is a set of partial classes, just to make this monster of a class more manageable; but I'm using Java, which doesn't support that.

Is there some standard practice for breaking down a large class like this? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire