mercredi 1 juin 2016

Is there a creational pattern for creating data objects based on a dependency?

I have a class that only contains data, but depends on a connection object from which the data is fetched. The connection object manages a low level socket connection to a much bigger API (old single header C API). Currently i have this:

public class MyData {
    private int data1;

    public MyData(Connection connection) {
         data1 = connection.getLowLevelConnection().someDataFetchCall();
    }

    // getters, setters
}

It works, but feels wrong. Intuitively i would say, that the data should be acquired in a function, which returns my initialized data object:

public class DataFetcher {
    public static MyData getMyDataFromConnection(Connection connection) { ... }
    // ... more data fetching functions
}

Is there a named pattern for this kind of task? That would make naming these classes easier.

Aucun commentaire:

Enregistrer un commentaire