Luca Fülbier
Luca Fülbier

Reputation: 2731

How to structure big classes?

I am currently writing a Java API for communicating with a device. The original API for the device provided by the devices manufacturer is written in C and is basically just one big header file defining every struct, enum and function necessary for communicating with the device. A lot of these structs and functions are actually for changing device Settings or retrieving device Information.

Since im porting this API to Java i wanted to make the API more OO-ish and more easy to use for someone who has no actual knowledge about the device and its C API. My Goal is to have one class Device which has methods for connecting and retrieving data from the device.

Now i am wondering how i should incorporate all the Settings and Information in my class. If i basically do an OO-Port of the original API the class would get reeeaaally big and harder to use as a result. What i did for a start was putting all the Settings Information in its own class and making that class a member of my Device class. The DeviceSettings class will have a reference to its Device classes Connection object (Basically an object that handles the communication with the C API through JNI/JNA).

It now looks something like this:

class Device {
    private JNIDeviceConnection connection;

    private DeviceSettings settings;
    private DeviceInformation information;
    private SpecificDeviceSettingsA spSettingsA;
    ...

    public void connect();
    public void disconnect();
    public DataObject getData();

    public Device() {
        settings = new DeviceSettings(connection);
        ...
    }
}

Using the class would look like this:

Device dev = new Device();
dev.connect();
dev.getDeviceSettings().changeSettingA(newValue);
dev.getData();
dev.disconnect();

Do you think this is a good approach to this kind of problem? I feel like the class does too much, but i would rather have a class doing too much than having something like this:

Device dev = new Device()
dev.connect()
DeviceSettingsUtility.changeSettingsA(dev, newValue);
...

Am i missing an obvious better alternative?

Upvotes: 1

Views: 97

Answers (2)

GhostCat
GhostCat

Reputation: 140623

My two cents: don't do that.

Don't put a single/static class monolith into java code, just to resemble some ancient C API. Because: that does add little to no value. And will become a maintenance nightmare immediately.

No serious java programmer will like to use your new "product"; and no serious C programmer will see the need to move to a new language if there is no real gain from it.

Long story short: don't waste your time "copying" what is already there. Instead, understand the requirements; and design a clean, easy to use, "state of the art" API that has the potential to attract people (and other maintainers that support you in the long run). Meaning: go full OO, provide reasonable abstractions, etc. ...

Upvotes: 2

Helios
Helios

Reputation: 851

If I had to structure this I would have done something like this below:

Model:

//With getter and setter and other details

Device : composed of {DeviceSettings, DeviceInformation}
DeviceSettings:
DeviceInformation:

Manager/Service:

ConnectionManager:
                |
                -getCnnection(Device device)
DeviceManager:
             |
             - connect(Device device)
             - disconnect(Device device)
             - changeSettings(Device device)

Usage :

DeviceManager.changeSettings(device, setting){
  //get the current setting and update
}

Upvotes: 0

Related Questions