0

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.

6
  • Sounds like the proxy pattern Commented Jun 1, 2016 at 12:17
  • Repository pattern. Commented Jun 1, 2016 at 15:56
  • Would it be out of place to call your example with the initialized object a "Factory" ? Commented Jun 1, 2016 at 21:12
  • @vikingsteve It kinda is a factory, but i feel like the name factory didnt really suit the purpose Commented Jun 1, 2016 at 21:42
  • 1
    There's always the approach new MyDataBuider.withConnection(connection).build(), but that also feels a little out of place. I think you're in a "grey" area here... ;) Commented Jun 2, 2016 at 6:47

2 Answers 2

2

You tied the data itself to its loading mechanism. Without proposing any pattern, you should decouple it. I guess there is no real pattern that you can apply. It's a matter of design principles especially the single responsibility principle and dependency inversion. The single responsibility principle says you should not separate things that belong together and you should not tie things that don't belong together. The dependency inversion principle says do not depend on concretes, depend on abstracts.

The design should finally come up with

  • one class to represent the data and
  • one class to load the data to satisfy the single responsibility principle.

  • If you want to be independent of the loading mechanism introduce abstraction (e.g. interface), applying dependency inversion.

It's true that the repository "pattern" defines such a structure.

A side note: I personally do not accept the label "pattern" on it. It's only a label for a benifical stucture that you will reach anyway if you apply the SOLID principles. In contrary the 23 design patterns from the GoF. They were not made up, they were identified. They have intrinsic meaning. The "pattern" label suggest that the repository "pattern" falls in the same category but it doesn't. It is lacking naturalness and atomicity.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually did the exact same thing while trying to come up with a better solution. I don't know if i like it better from a user standpoint, but it certainly makes testing a lot easier (because i can mock more parts of the system separately, thanks to interfaces and DI). The biggest problem for my design is packaging the C API (since it breaks all the SOLID principles from the get go, being a single header, non-OO library) into a Java library, that doesn't feel too C'ish. Thank you for the suggestions, i guess i will read up on Dependendy Inversion now!
1

It works, but feels wrong.

It does.

Sticking to DDD, I'd create a repository if MyData was an aggregate or a service if MyData was an entity. Anyway I'd put classes to infrastructure layer and interfaces with MyData to domain layer. For example,

MyData entity:

package myapp.domain.model;

public class MyData {
    private int data1;
    // other fields

    public MyData(int data1) {
        this.data1 = data1;
    }

    // other methods
}

Service interface:

package myapp.domain.service;

import myapp.domain.model.MyData;

public interface MyService {
    MyData GetMyData();
}

Service implementation:

package myapp.infrastructure.oldapi;

import myapp.domain.model.MyData;
import myapp.domain.service.MyService;

public class MyServiceImpl implements MyService {
    @Override
    public MyData GetMyData() {
        MyData myData = connection.getLowLevelConnection().someDataFetchCall();
        return myData;
    }
}

1 Comment

A very nice addition to the accepted answer, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.