S
S
synapse_people2018-02-22 12:25:18
Java
synapse_people, 2018-02-22 12:25:18

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);
}

It has the interface itself, as well as the AttrKey class right away ...
And also ConfigImpl in the test.core package of the core-impl project:
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);
      }

    };
  }

}

Tell me, is this correct?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question