Answer the question
In order to leave comments, you need to log in
How to properly separate API and implementation?
Hello!
There are 2 projects: core-api, core-impl. In the API, I have sets of interfaces, and in impl, the implementation of these interfaces. They have a common package - test.core.
So the question is, is this correct?
Let's take the configuration class as an example...
The interface in the test.core package is the core-API project:
public interface ICfg {
public static final class AttrKey<V> {
private final String name;
private final Class<V> dataType;
private AttrKey(String name, Class<V> dataType) {
super();
this.name = name;
this.dataType = dataType;
}
public String getName() {
return name;
}
public Class<V> getDataType() {
return dataType;
}
public static <V> AttrKey<V> newKey(String name, Class<V> dataType) {
return new AttrKey<V>(name, dataType);
}
}
public interface Attribute<V> {
public Optional<V> getValue();
public void setValue(V value);
}
public <V> Attribute<V> attr(AttrKey<V> key);
}
public final class Config implements ICfg {
private final Map<String, Object> values = new HashMap<>();
private Config() {
}
public static ICfg newInstance() {
return new Config();
}
@Override
public <V> Attribute<V> attr(AttrKey<V> key) {
return new Attribute<V>() {
@Override
public Optional<V> getValue() {
final V value;
value = key.getDataType().cast(values.get(key.getName()));
return Optional.of(value);
}
@Override
public void setValue(V value) {
values.put(key.getName(), value);
}
};
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question