Should a class have a "has-a" relationship with a configuration class?

Viewed 96

I have seen some questions about wrapping a set of configuration properties in a class, but not how to actually use it. Given a configuration class (omitting getters and setters for brevity):

class ServiceConfiguration {
    private String foo;     
}

Should the service class use the configuration directly?

static class SomeServiceB {
    private ServiceConfiguration configuration;

    public SomeServiceB(ServiceConfiguration configuration) {
        this.configuration = configuration;
    }

    public void printIt() {
        System.out.println(configuration.getFoo());
    }
} 

Or should it only be concerned with the actual value of foo? For example:

static class SomeServiceA {
    private String foo;

    public SomeServiceA(String foo) {
        this.foo = foo;
    }

    public void printIt() {
        System.out.println(foo);
    }
}

I would argue that the SomeServiceA is superior because of the lower coupling and separation of concerns.

1 Answers
Related